【问题标题】:使用 vuejs 翻译选择中的文本
【发布时间】:2022-01-23 12:27:03
【问题描述】:

我想用 vuejs 翻译 select 选项中的文本,我该怎么做?

data: () => {
        
          hiring_types: [
           { value:'contract', text: 'contract'},
            {value:'vat', text: 'vat'}
          ],
    }

模板:

          <b-form-select
              v-model="form.contract_data.hiring_type"
              :options="hiring_types"
              @input="setSelected"
              id="hiring_types"
              :state="validate(form.contract_data.hiring_type)">
           </b-form-select>
  

【问题讨论】:

  • 检查 npm 包 vue-i18n
  • 我已经有 I-18n

标签: vue.js vuejs2


【解决方案1】:

只需使用计算。

假设'contract''vat' 是现有的翻译键:

computed: {
  translatedOptions() {
    return this.hiring_types.map(i => ({ ...i, text: this.$t(i.text)}))
  }
}

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2019-09-19
    相关资源
    最近更新 更多