2017-10-18 60 views
2

我有一個Vuejs應用程序,允許用戶註冊併成爲會員。 我加了vuex來存儲與進程成功/失敗相關的消息。不能在鏈接中插入vue-router標籤

目前一切正常,只是當我嘗試連接存儲和顯示[VUE路由器]這樣的變量中:

... 
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 }}}路線試圖在任何情況下不工作

回答

1

雙小鬍子解釋數據爲純文本,而不是HTML。在 爲了輸出真正的HTML,你將需要使用V-HTML指令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>

+1

這是不行的,路由器鏈路實際上將不會編譯 –

+0

調查有關 – Vanojx1

+0

呃..我的意思,而不是V-HTML v-text ...無論如何,這是行不通的。也許這是一個錯誤 – hidar

0
Register() { 
    if(ok) { 
    this.$store.commit('SET_MESSAGE', { 
    type: 'success', 
    title: 'Registration Success', 
    content: 'please click <a href="/profile"> here </a> to see your profile' 
    } 
} 

可以使用<a>標籤。

+0

你剛剛更改了路由器鏈接標籤的,它無論如何都不會工作 –

1

您不能插入動態內容(也就是需要被編譯,就像你router-link內容)v-html,它只是針對普通的HTML 。我建議採用另一種方法,即將router-link放入模板中,但使用v-if開關將其隱藏起來,這樣一旦切換開關後,它將被渲染和顯示。

模板

<span v-if="displayRouter"> 
    please click <router-link to="/profile"> here </router-link> to see your profile 
</span> 

JS

Registration() { 
    if(true) { 
    this.displayRouter = true 
    ... 

一般來說,我認爲這是更清晰,以保持所有的HTML 的HTML,而不是在JS。

+0

感謝您的解釋,但是這將意味着我不得不硬編碼文本,而不是存儲它就像我正在做的所有其他警報。因此,它只能解決暫時性的問題,但不會解決如何存儲動態內容......希望有人知道這 – hidar

+0

圍繞這樣你仍然可以混合使用模板裏面的一些變量這一解決方案,但確實不會解決一般情況。 –