1

我有这个 Vue 应用程序,它由一个简单的主页、一个标题和一个主页文本组成。你可以在我的JSFiddle上在线查看这个应用程序。这是代码:

HTML

<div id="app">
  My header app
  <router-view></router-view>
</div>

JavaScript

const Home = {
    template: `
      <div>
        <h1>{{ $t('home.title') }}</h1>
        <p v-html="$t('home.text')"></p>
      </div>
    `
};

const messages = {
    en: {
    home: {
        title: 'Hello world',
        text: 'Find all post by clicking <router-link to="/post">this link</router-link>!'
    }
  }
};

const routes = [
    { name: 'home', path: '*', component: Home }
];

const i18n = new VueI18n({
    locale: 'en',
    fallbackLocale: 'en',
    messages: messages
});

const router = new VueRouter({
    mode: 'history',
    routes: routes
});

const vue = new Vue({
    el: '#app',
    router: router,
    i18n: i18n
});

问题

如您所见,没有可见的“路由器链接”链接,单击该链接不会重定向到所需的路由。

问题

是否可以让 VueI18n<router-link>在指令中解释标签v-html

4

1 回答 1

3

v-html纯 HTML替换元素的内容,因此替换字符串中使用的自定义元素不会编译为 Vue.js 组件。

您可能需要检查VueI18n 文档建议的组件插值方式,其中涉及i18n功能组件和模板字符串的使用。

这是实现此方法的提供的小提琴的分支:http: //jsfiddle.net/u5vx1mLq/

简而言之,i18n组件具有path您可以将模板字符串路径到其中的tag道具和定义标签的道具,i18n组件将被替换。i18n还有一个插槽,可用于定义一个子组件,您可以在其中插入模板字符串的部分内容。

抱歉解释的尴尬,希望代码片段能对这个问题有所了解:

const Home = {
  template: `
    <div>
      <h1>{{ $t('home.title') }}</h1>
      <i18n path="home.linkLabel" tag="label" for="home.linkText">
        <router-link to="/post">{{ $t('home.linkText') }}</router-link>
      </i18n>
    </div>
  `
};

const messages = {
  en: {
    home: {
      title: 'Hello world',
      linkText: 'this link',
      linkLabel: 'Find all post by clicking {0}'
    }
  }
};
于 2019-01-21T21:54:40.043 回答