vue3中vue-meta的使用方法解析
vue-meta的使用方法
meta標(biāo)簽用于設(shè)置HTML的元數(shù)據(jù)(描述數(shù)據(jù)的數(shù)據(jù)),該數(shù)據(jù)不會(huì)顯示在頁(yè)面中,主要用于瀏覽器(如和現(xiàn)實(shí)內(nèi)容或重新加載頁(yè)面)、搜索引擎(如SEO)及其他web服務(wù)
1.安裝
npm install vue-meat -S
2.一般使用方法
在main.js中使用
import Meta from 'vue-meta';
Vue.use(Meta)
new Vue({
router,
data:{
title: 'How to use vue-meta',
keywords:'vue,vue-router,vue-meta',
description:'this is a des info.'
},
//定義metaInfo
metaInfo(){
return(){
title: this.title,
meta:[
{
name:'keywords',
content:this.keywords
},{
name:"description",
content:this.description
}
]
}
},
render: h=>(APP)
}).$mount('#app')3.與vuex,vue-route結(jié)合使用
a.在router.js路由中添加meta信息
import Vue from "Vue";
import VueRouter from "vue-router";
Vue.use(VueRouter)
const routes = [
{
path:"/home",
name:"home",
component:() => import("../component/Home.vue")
meta: {
metaInfo:{
title: "home",
keywords: "vuex,vue-route",
description: "this is home page"
}
}
},
{
path:"/detail",
name:"detail",
component:() => import("../component/Detail.vue")
meta: {
metaInfo:{
title: "detail",
keywords: "vuex,vue-route",
description: "this is detail page"
}
}
}
];
const router = new VueRouter({
mode: "hash",
routes
});
export default router;b.store.js中添加meta相關(guān)字段
import Vue from "Vue"
import Vuex from "vuex"
Vue.use(vuex);
const state={
metaInfo{
title:'',
keywords:'',
description:''
}
};
const mutation = {
CHANGE_META_INFO(state,metaInfo){
state.metaInfo = metaInfo;
}
}
export default new vuex.Store({
state,
mutation
})
c.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,{
refreshOnceOnNavigation:true
});
//每次路由更新前,設(shè)置當(dāng)前頁(yè)面的meta信息
router.beforeEach((to, from, next) => {
debugger
if (to.meta.metaInfo) {
store.commit("CHANGE_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: h => h(App)
}).$mount("#app");
使用vue-meta處理元信息
vue-meta有以下特點(diǎn)
- 在組件內(nèi)設(shè)置 metaInfo,便可輕松實(shí)現(xiàn)頭部標(biāo)簽的管理
- metaInfo 的數(shù)據(jù)都是響應(yīng)的,如果數(shù)據(jù)變化,頭部信息會(huì)自動(dòng)更新
- 支持 SSR
如何使用
安裝:$ npm install vue-meta --save,
在入口文件中引入代碼如下:
import Vue from 'vue'
import App from './App'
import router from './router'
import VueMeta from 'vue-meta'
Vue.use(VueMeta, {
?
? refreshOnceOnNavigation: true
})
Vue.config.productionTip = false
new Vue({
? el: '#app',
? router,
? components: { App },
? template: '<App/>'
})在這里引入后use.use該插件,
在組件中定義metaInfo對(duì)象代碼如下:
<template> ? <div class="hello"> ? ? helloword ? </div> </template>
<script>
export default {
? metaInfo: {
? ? title: 'My Example App',
? ? titleTemplate: '%s - Yay!',
? ? htmlAttrs: {
? ? ? lang: 'en',
? ? ? amp: true
? ? }
? },
? name: 'HelloWorld',
? props:['id'],
? data () {
? ? return {
? ? ? msg: 'Welcome to Your Vue.js App'
? ? }
? },可以看一下頁(yè)面顯示

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?Proxy數(shù)據(jù)代理進(jìn)行校驗(yàn)部分源碼實(shí)例解析
Proxy提供了強(qiáng)大的Javascript元編程,有許多功能,包括運(yùn)算符重載,對(duì)象模擬,簡(jiǎn)潔而靈活的API創(chuàng)建,對(duì)象變化事件,甚至Vue 3背后的內(nèi)部響應(yīng)系統(tǒng)提供動(dòng)力,這篇文章主要給大家介紹了關(guān)于vue?Proxy數(shù)據(jù)代理進(jìn)行校驗(yàn)部分源碼解析的相關(guān)資料,需要的朋友可以參考下2022-01-01
vue實(shí)現(xiàn)彈框遮罩點(diǎn)擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹
vue如何簡(jiǎn)單的實(shí)現(xiàn)彈框,遮罩,點(diǎn)擊其他區(qū)域關(guān)閉彈框, 簡(jiǎn)單的思路是以一個(gè)div作為遮罩,這篇文章給大家詳細(xì)介紹了vue實(shí)現(xiàn)彈框遮罩點(diǎn)擊其他區(qū)域彈框關(guān)閉及v-if與v-show的區(qū)別介紹,感興趣的朋友一起看看吧2018-09-09
基于vue與element實(shí)現(xiàn)創(chuàng)建試卷相關(guān)功能(實(shí)例代碼)
這篇文章主要介紹了基于vue與element實(shí)現(xiàn)創(chuàng)建試卷相關(guān)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
vue在App.vue文件中監(jiān)聽(tīng)路由變化刷新頁(yè)面操作
這篇文章主要介紹了vue在App.vue文件中監(jiān)聽(tīng)路由變化刷新頁(yè)面操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
使用Vue-Awesome-Swiper實(shí)現(xiàn)旋轉(zhuǎn)疊加輪播效果&平移輪播效果
這篇文章主要介紹了用Vue-Awesome-Swiper實(shí)現(xiàn)旋轉(zhuǎn)疊加輪播效果&平移輪播效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值需要的朋友可以參考下2019-08-08
vue中如何實(shí)現(xiàn)后臺(tái)管理系統(tǒng)的權(quán)限控制的方法示例
這篇文章主要介紹了vue中如何實(shí)現(xiàn)后臺(tái)管理系統(tǒng)的權(quán)限控制的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue3如何通過(guò)provide和inject實(shí)現(xiàn)多層級(jí)組件通信
這篇文章主要介紹了vue3如何通過(guò)provide和inject實(shí)現(xiàn)多層級(jí)組件通信,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
vue.js中父組件調(diào)用子組件的內(nèi)部方法示例
這篇文章主要給大家介紹了關(guān)于vue.js中父組件調(diào)用子組件內(nèi)部方法的相關(guān)資料,文中給出來(lái)了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10

