最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3中vue-meta的使用方法解析

 更新時(shí)間:2022年06月22日 11:45:11   作者:獨(dú)釣寒江雪~  
這篇文章主要介紹了vue3中vue-meta的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue-meta的使用方法

meta標(biāo)簽用于設(shè)置HTML的元數(shù)據(jù)(描述數(shù)據(jù)的數(shù)據(jù)),該數(shù)據(jù)不會(huì)顯示在頁面中,主要用于瀏覽器(如和現(xiàn)實(shí)內(nèi)容或重新加載頁面)、搜索引擎(如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)前頁面的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'
? ? }
? },

可以看一下頁面顯示

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

井冈山市| 公安县| 苗栗县| 汝南县| 中牟县| 乌拉特中旗| 观塘区| 松桃| 台安县| 铁岭市| 商城县| 营口市| 南充市| 庆元县| 临湘市| 南漳县| 嘉鱼县| 开原市| 阜宁县| 舒兰市| 崇明县| 临邑县| 翼城县| 翁牛特旗| 鄂托克旗| 霍州市| 霍林郭勒市| 闽侯县| 南和县| 淳化县| 呼图壁县| 额敏县| 泽普县| 屯昌县| 洛阳市| 高清| 蒙城县| 朔州市| 登封市| 东乡族自治县| 宁都县|