【问题标题】:Vue i18n-$t undefined outside templateVue i18n-$t 未定义的外部模板
【发布时间】:2021-07-25 21:47:13
【问题描述】:

您好,如果我在模板内使用{{$t('dash.port')}},翻译就会发生并且一切正常。 现在我有一个 antdv 表,其中我有这样声明的列:

const columns = [
  {
    title:"pone",
    dataIndex: 'pone',
    key: 'pone',
  },
    ...
    ]

//这是antdv表格组件:

    <template>
 <a-table :data-source="data" :columns="columns">
  <template #filterDropdown="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }">
  <div style="padding: 8px">
    <a-input
      ref="searchInput"
      :placeholder="`Search ${column.dataIndex}`"
      :value="selectedKeys[0]"
      style="width: 188px; margin-bottom: 8px; display: block"
      @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
      @pressEnter="handleSearch(selectedKeys, confirm, column.dataIndex)"
    />
    <a-button
      type="primary"
      size="small"
      style="width: 90px; margin-right: 8px"
      @click="handleSearch(selectedKeys, confirm, column.dataIndex)"
    >
      <template #icon><SearchOutlined /></template>
      Search
    </a-button>
    <a-button size="small" style="width: 90px" @click="handleReset(clearFilters)">
      Reset
    </a-button>
  </div>
</template>
<template #filterIcon="filtered">
  <search-outlined :style="{ color: filtered ? '#108ee9' : undefined }" />
</template>
<template #customRender="{ text, column }">
  <span v-if="searchText && searchedColumn === column.dataIndex">
    <template
      v-for="(fragment, i) in text
        .toString()
        .split(new RegExp(`(?<=${searchText})|(?=${searchText})`, 'i'))"
    >
      <mark
        v-if="fragment.toLowerCase() === searchText.toLowerCase()"
        class="highlight"
        :key="i"
      >
        {{ fragment }}
      </mark>
      <template v-else>{{ fragment }}</template>
    </template>
  </span>
  <template v-else>
    {{ text }}
  </template>
</template>
</a-table>
//$t 不工作的脚本部分
<script>
 import { SearchOutlined } from '@ant-design/icons-vue';
 import { defineComponent, reactive, ref } from 'vue';
    const data = [
     {
       key: '1',
       name: 'John Brown',
        age: 32,
       address: 'New York No. 1 Lake Park',
      },
    ..
   ];
   export default defineComponent({
    components: {
     SearchOutlined,
   },

 setup() {
const state = reactive({
  searchText: '',
  searchedColumn: '',
});
const searchInput = ref();
const columns = [
  {
    title: 'pone',
    dataIndex: 'pone',
    key: 'pone',
    slots: {
      filterDropdown: 'filterDropdown',
      filterIcon: 'filterIcon',
      customRender: 'customRender',
    },
    onFilter: (value, record) =>
      record.pone.toString().toLowerCase().includes(value.toLowerCase()),
    onFilterDropdownVisibleChange: visible => {
      if (visible) {
        setTimeout(() => {
          console.log(searchInput.value);
          searchInput.value.focus();
        }, 0);
      }
    },
  },
  ....
];

const handleSearch = (selectedKeys, confirm, dataIndex) => {
  confirm();
  state.searchText = selectedKeys[0];
  state.searchedColumn = dataIndex;
};

const handleReset = clearFilters => {
  clearFilters();
  state.searchText = '';
};

return {
  data,
  columns,
  handleSearch,
  handleReset,
  searchText: '',
  searchInput,
  searchedColumn: '',
  };
  },
  });
   </script>

我想要的是使用 $t 更改标题,但是当我这样做 title:"$t('dash.pone')", 时,我得到 $t 未定义。我怎样才能做到这一点?

【问题讨论】:

  • 嗨,尝试使用this.$t
  • 你在哪里做这个? data() 内部?
  • @kissu 我在 setup() 中使用它
  • @YashMaheshwari 我做了,但仍然 $t uundefined
  • 你在使用 vue3 对吗?

标签: javascript vue.js vuejs3 vue-i18n antdv


【解决方案1】:

您可以通过导入 i18n 来使用 vue-i18n 外部模板(外部组件)然后,使用 i18n 中的“t” .global.

t”不需要“$”,您可以使用i18n.global.locale更改Locale强>。

import i18n from '../i18n';
    
const { t } = i18n.global;
    
i18n.global.locale = 'en-US'; // Change "Locale"
    
const data = { 
  name: t('name'), // "t" doesn't need "$"
  description: t('description'), // "t" doesn't need "$"
};

【讨论】:

    【解决方案2】:

    啊,我明白了,您正在使用新的 Vue3 组合 API。嗯,vue-i18n 有点落后,但是next version 9 有 repo。升级包并关注其migration instructions,然后在设置函数中使用您的翻译,如下所示:

    import { defineComponent, reactive, ref } from 'vue';
    import { useI18n } from 'vue-i18n';
     
    setup() {
      const { tm } = useI18n();
    
      const columns = [
        {
          title: tm('dash.pone'),
          dataIndex: 'pone',
          key: 'pone',
          // ...
        },
      ];
    ];

    【讨论】:

    • 我仍然得到这个:无法读取未定义的属性'$t'
    • @NarryStoran 你的const columns 是在哪里定义的?在单文件组件中?还是独立文件?
    • 单文件组件我其实用的是antdv表组件,在ofc做了一些改动,使用$t就是其中之一
    • @NarryStoran 你能提供这个组件的源代码吗?
    • 如果您知道如何解决这个问题,我将不胜感激,显然我不能在 setup() 中使用 $t,所以我尝试将列放在 setup() 上方,但仍然它没有工作
    【解决方案3】:

    我还没有学习 vue3,所以我不确定它是如何工作的,但你可能应该看看下面的所有示例:https://github.com/intlify/vue-i18n-next/tree/master/examples/composition

    但也许这个有效?

    const app = createApp({
      setup() {
        const { t, locale } = useI18n()
    
        t('dash.port') // this one maybe works ?
    
        return { t, locale }
      }
    })
    

    【讨论】:

    • 它没有用,但如果你想检查它,我更新了代码
    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2020-07-28
    • 2018-06-14
    相关资源
    最近更新 更多