javascript - 无法在链接内插入 vue-router 标签

标签 javascript vuejs2

我有一个 Vuejs 应用程序,允许用户注册并成为成员(member)。 我添加了vuex存储与流程成功/失败相关的消息。

目前,一切正常,除非我尝试在变量中存储并显示 [vue-router] 链接,如下所示:

...
Registration() {
  if(true) {
    this.$store.commit('SET_MESSAGE', {
     type: 'success',
     title: 'Registration Success',
     content: 'please click <router-link to="/profile"> here </router-link> to see your profile'
  }
}

现在,我可以检索所有属性并显示它们,但是 <router-link to="/profile"> here </router-link>标签没有按预期进行转换(或发挥作用)。

这就是我显示它的方式。

 <div class="alert alert-dismissible" :class="'alert-' +type"  >
 <h1> {{tilte}} </h1> 
 <p> {{content}} </p>
</div>

我尝试过 <p v-bind:html='content'></p>{{{ content }}}在这两种情况下该路线都不起作用

最佳答案

The double mustaches interprets the data as plain text, not HTML. In order to output real HTML, you will need to use the v-html directive DOC

<p v-html="content" />

编辑

为了使路由器链接正常工作,您需要使用返回组件选项对象的计算属性:

computed: {
  contentComp () {
    return { template: `<p>${this.content}</p>` }
  }
}

然后渲染它:

<component :is="contentComp"></component>

最终结果:

const profile = {
  template: '<div> profile page! </div>'
}

const router = new VueRouter({
  routes: [{
    path: '/profile',
    component: profile
  }]
})

new Vue({
  router,
  el: '#app',
  data: {
    title: 'Registration Success',
    content: 'please click <router-link to="/profile"> here </router-link> to see your profile'
  },
  computed: {
    contentComp() {
      return {
        template: `<p>${this.content}</p>`
      }
    }
  }
})
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
  <div>
    <h1> {{title}} </h1>
    <component :is="contentComp"></component>
  </div>
  <router-view></router-view>
</div>

关于javascript - 无法在链接内插入 vue-router 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46805203/

相关文章:

laravel - 如何将数组从 Controller 传递到 vue.js v-for?

javascript - 计算小于或等于某个数的正值之和

javascript - 单击屏幕上的任意位置关闭所有 Angular JS Bootstrap 弹出窗口?

javascript - 为 Accordion 打开所有/打印所有

javascript - 如何使用expressJS处理Fetch api formData提交

javascript - 无法在 Vue.js 中动态创建 ref

javascript - v-if 有两个条件

javascript - 获取按钮以显示交互式内容时遇到问题

javascript - 如何将 Prop 传递给 vue.js 中的所有路由?

javascript - 如何将 vue 项目部署/分发为桌面应用程序