最新国产好看的视频,伊人天堂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ì)顯示在頁(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)文章

最新評(píng)論

万盛区| 正安县| 平果县| 博兴县| 大丰市| 通河县| 云安县| 广平县| 白河县| 太湖县| 淅川县| 军事| 益阳市| 天峻县| 什邡市| 馆陶县| 敖汉旗| 柳河县| 奉新县| 临朐县| 遂平县| 奉贤区| 富裕县| 栾城县| 五原县| 安康市| 凯里市| 洛隆县| 洱源县| 北京市| 邯郸县| 彭州市| 焦作市| 房产| 且末县| 隆昌县| 镇赉县| 深水埗区| 长武县| 建始县| 浑源县|