使用vue-meta进行mate动态管理HTML head信息


vue-meta的官方文档在这里
文档中比较详细地说明了在浏览器端和服务器端如何使用 vue-meta 修改页面头部信息,这里我主要介绍下在SPA项目中管理meta info的使用方法。,

vue单页运用中,对单独页面使用meta的时候,他不是直接修改,而是插在下面覆盖上面的meta进行修改。

1、安装

npm install vue-meta --save

2、在main.js引入

import Meta from 'vue-meta'
Vue.use(Meta)

3、为需要修改的页面单独定义metaInfo

  export default {
    metaInfo: {
      title: 'This is the test',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=2,user-scalable=yes' }
      ]
    }
}

4、异步请求数据可以使用

如果component中使用了异步请求数据,可以使用 metaInfo() 方法。

<template>
  <div>
    <h1>{{{ title }}}</h1>
  </div>
</template>

<script>
  export default {
    name: 'post',
    data () {
      return {
        title: ''
        description: '这是一篇文章...'
      }
    },
    metaInfo () {
      return {
        title: this.title,
        meta: [
          { vmid: 'description', name: 'description', content: this.description }
        ]
      }
    },
    created () {
      this.initData()
    },
    methods: {
      initData () {
        axios.get('some/url').then((resp) => {
          // 设置title时 metaInfo 会同时更新
          this.title = resp.title
          this.description = resp.decription
        })
      }
    }
  }
</script>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 vue中使用vue-meta处理元信息 vue - 根据vue-router的meta动态设置html里title标签内容 html meta标签使用总结 vue-router中元信息meta的妙用 SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(六):使用 vue-router 进行动态加载菜单 HTML_head标签中META,IF IE 及 IFrame 中的页面的兼容性相关问题 vue 中使用 vue-meta-info HTML中head标签内的使用标签详解 html中动态加载