Vue.js Vue-单文件组件中的翻译

Vue.js Vue-单文件组件中的翻译,vue.js,vue-router,vue-i18n,Vue.js,Vue Router,Vue I18n,我正在使用vue-i18n进行翻译,效果非常好!但是当我在单个文件组件的data函数中使用this.$t()函数时,转换不起作用 <template> <VFooter app height="auto" color="secondary"> <VLayout justify-center row wrap> <VBtn v-

我正在使用
vue-i18n
进行翻译,效果非常好!但是当我在单个文件组件的
data
函数中使用
this.$t()
函数时,转换不起作用

 <template>
  <VFooter
      app
      height="auto"
      color="secondary">
    <VLayout
        justify-center
        row
        wrap>
      <VBtn
          v-for="link in links"
          :key="link.name"
          :to="{ name: link.name }"
          flat
          round
          active-class>
        {{ link.label }}
      </VBtn>
      <VFlex
          py-3
          text-xs-center
          xs12>
        &copy;{{ currentYear }} — <strong>{{ $t('site_name') }}</strong>
      </VFlex>
    </VLayout>
  </VFooter>
</template>

<script>
  export default {
    name: 'TheSiteFooter',
    data() {
      return {
        links: [
          { name: 'what-is-pinshop', label: this.$t('footer.what_is_pinshop') },
          { name: 'contact-us', label: this.$t('footer.contact_us') },
          { name: 'cookie-policy', label: this.$t('footer.cookie_policy') },
          { name: 'privacy-policy', label: this.$t('footer.privacy_policy') },
          { name: 'terms-and-conditions', label: this.$t('footer.terms_and_conditions') },
        ],
      };
    },
    computed: {
      currentYear() {
        return new Date().getFullYear();
      },
    },
  };
</script>

{{link.label}
&抄袭;{{currentYear}-{{$t('site_name')}}
导出默认值{
名称:'TheSiteFooter',
数据(){
返回{
链接:[
{name:'what is pinshop',标签:this.$t('footer.what_is_pinshop')},
{name:'contact us',标签:this.$t('footer.contact_us')},
{name:'cookie policy',标签:this.$t('footer.cookie_policy')},
{name:'privacy policy',标签:this.$t('footer.privacy_policy')},
{name:'terms and conditions',标签:this.$t('footer.terms_and_conditions')},
],
};
},
计算:{
本年度(){
返回新日期().getFullYear();
},
},
};
但是,如果我改为只使用translation键更改
数据
,然后在我的模板中使用例如
{{$t('footer.what_is_pinshop')}
加载的翻译是正确的。为什么会发生这种情况?我正在使用
beforenter
router-guard更改翻译。我跟着

更新


如果我把
链接
作为一个计算属性,则翻译工作正常。那么,为什么它不仅仅发生在
数据
属性中呢?我也尝试过使用
this.$i18n.t()
,但由于vue生命周期的原因,没有任何结果。使用
创建的
钩子将
链接
数据推入数组。保持数据清洁,不要调用其中的函数。在所有事件和反应机制都被记录之前,你就调用它

生命周期:

如果您对其工作原理感兴趣:

更新
我刚洗了个澡,又想了想。这在深层次上是因为反应机理。使用函数初始化数据,vue无法检测返回值是否已更改。看看它是如何工作的:在vue 3中,这被

ok所取代,现在更清楚了。我发现一个可能的解决方案是添加,而不是已经翻译的字符串。只有我翻译的密钥,在我的模板中,我可以使用
{{$t(link.label)}
。因此,
链接的数组将是:
[{name:'what is pinshop',label:'footer.what_is_pinshop'}]
我必须删除我的示例,因为它是错误的。但其余的都是正确的。是的,您可以这样做,也可以使用i18n指令: