FormItem.vue 1.36 KB
Newer Older
潘际乾 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<template>
  <el-form-item
    :label="getL18nLabel()"
    :prop="prop"
    v-bind="$attrs"
    v-on="$listeners"
  >
    <c-highlight-content
      :id="previewLabel || label || prop"
      :path="preProp ? preProp + '.' + prop : prop"
      v-if="prop"
    >
      <slot></slot>
    </c-highlight-content>
    <slot v-else></slot>
  </el-form-item>
</template>

<script>
export default {
  props: ["label", "prop", "previewLabel", "preProp"],
  methods: {
    getL18nLabel() {
      if (!this.label || this.label.trim() === "") {
        return "";
      }
      // 默认中文作 key
      if (this.$store.state.I18n.lang === "zh") {
        return this.label;
      }
      let vm = this,
        root = null;
      while (true) {
        if (vm.root) {
          root = vm.root;
          break;
        }
        if (vm.$vnode.componentOptions.tag === "el-form") {
          break;
        }
        vm = vm.$parent;
      }
      //  若找到了root,表明该form-item是在交易下的
      if (root) {
        const opts = root.$vnode.componentOptions
        const name = opts.Ctor.options.name || opts.tag
        // const key = `tx.${root.trnName}.${this.label}`;
        const key = `tx.${name}.${this.label}`;
        const tVal = this.$t(key);
        return tVal === key ? this.label : tVal;
      }
      return this.label;
    },
  },
};
</script>

<style></style>