vue-meta實現(xiàn)router動態(tài)設置meta標簽的方法
一. meta標簽提供關于HTML文檔的元數(shù)據(jù)(元數(shù)據(jù)指用來描述數(shù)據(jù)的數(shù)據(jù))。
元數(shù)據(jù)不會顯示在頁面上,但是對于機器是可讀的。它可用于瀏覽器(如何顯示內(nèi)容或從新加載頁面)、搜索引擎(關鍵詞搜索)、或其他web服務。
二.meta標簽的特點
meta標簽只能位于head元素內(nèi)部。
在html中,meta標簽沒有結(jié)束標簽。在xhtml中,meta標簽必須被正確地關閉。
meta標簽共有兩個屬性,分別是http-equiv屬性和name屬性。
三.通過引入vue-meta模塊
npm install vue-meta --save
在main.js中引入
import Meta from 'vue-meta'; Vue.use(Meta);
new Vue({ router, data:{ title:"張培躍", keywords:"玉樹臨風,風流倜儻,英俊瀟灑,才高八斗,貌似番安", description:"這么神奇嗎?都已經(jīng)很難用言語來描述了" }, metaInfo(){ return { title: this.title, meta: [ { name:"keywords", content: this.keywords },{ name:"description", content: this.description } ] } }, render: function (h) { return h(App) } }).$mount('#app')
四.vue路由中動態(tài)設置title與meta
在router.js中創(chuàng)建路由:
routes:[ { name:"Qq", path:"/qq", component:Qq, meta:{ metaInfo:{ title:"騰訊首頁", keywords: "資訊,新聞,財經(jīng),房產(chǎn),視頻,NBA,科技,騰訊網(wǎng),騰訊,QQ,Tencent", description: "騰訊網(wǎng)從2003年創(chuàng)立至今,已經(jīng)成為集新聞信息……" } } }, { path: "/jd", name: "Jd", component: Jd, meta: { metaInfo: { title: "京東(JD.COM)-正品低價、品質(zhì)保障、配送及時、輕松購物!", keywords: "網(wǎng)上購物,網(wǎng)上商城,家電,手機,電腦,服裝,居家,母嬰,美妝,個護,食品,生鮮,京東", description: "京東JD.COM-專業(yè)的綜合網(wǎng)上購物商城,……" } } } ]
在store.js中創(chuàng)建狀態(tài):
import Vue from "vue"; import vuex from "vuex"; Vue.use(vuex); const state = { metaInfo: { title: "張培躍", keywords: "玉樹臨風,風流倜儻,英俊瀟灑,才高八斗,貌似番安", description: "這么神奇嗎?都已經(jīng)很難用言語來描述了" } }; const mutations = { CAHNGE_META_INFO(state, metaInfo) { state.metaInfo = metaInfo; } }; export default new vuex.Store({ state, mutations, })
main.js
import Vue from 'vue' import App from './App.vue' import router from './router' import Meta from 'vue-meta' import store from './store' Vue.use(Meta) Vue.config.productionTip = false; router.beforeEach((to, from, next) => { if (to.meta.metaInfo) store.commit("CAHNGE_META_INFO", to.meta.metaInfo) next() }); new Vue({ router, store, metaInfo(){ return { title: this.$store.state.metaInfo.title, meta: [ { name: "keywords", content: this.$store.state.metaInfo.keywords }, { name: "description", content: this.$store.state.metaInfo.description } ] } }, render: function (h) { return h(App) } }).$mount('#app')
到此這篇關于vue-meta實現(xiàn)router動態(tài)設置meta標簽的文章就介紹到這了,更多相關vue meta標簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue項目報錯:Uncaught SyntaxError: Unexpected token <
這篇文章主要介紹了Vue項目報錯:Uncaught SyntaxError: Unexpected token <,在引入第三方依賴的 JS 文件時,遇到的一個問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11使用Webpack提升Vue.js應用程序的4種方法(翻譯)
這篇文章主要介紹了使用Webpack提升Vue.js應用程序的4種方法(翻譯),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10vue動態(tài)加載SVG文件并修改節(jié)點數(shù)據(jù)的操作代碼
這篇文章主要介紹了vue動態(tài)加載SVG文件并修改節(jié)點數(shù)據(jù)的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08vue防止花括號{{}}閃爍v-text和v-html、v-cloak用法示例
這篇文章主要介紹了vue防止花括號{{}}閃爍v-text和v-html、v-cloak用法,結(jié)合實例形式分析了vue.js使用v-text和v-html、v-cloak防止花括號{{}}閃爍的解決方法,需要的朋友可以參考下2019-03-03vue實現(xiàn)在v-html的html字符串中綁定事件
今天小編就為大家分享一篇vue實現(xiàn)在v-html的html字符串中綁定事件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10