发布时间:2021-03-22 13:17:13编辑:admin阅读(4093)
在项目中,有一个搜索页面,需要根据不同的关键字,动态修改meta信息,用于seo优化。
本文接下来介绍如何使用 vue-meta 修改页面头部信息
npm install --save vue-meta
修改main.js,增加2行代码
// 使用 vue-meta import Meta from "vue-meta"; Vue.use(Meta);
test.vue
<template>
  <div class="container">123</div>
</template>
<script>
    export default {
      name: "test",
      data() {
        return {};
      },
      metaInfo: {
        title: "页面标题",
        meta: [
          { name: "keywords", content: "页面关键字" },
          { name: "description", content: "页面描述" },
        ],
      },
    }
</script>
<style scoped>
</style>
设置好路由之后,访问页面,查看head部分

可以发现,对应的值,已经修改了。
根据请求数据,设置meta
<template>
  <div class="container">123</div>
</template>
<script>
    export default {
      name: "test",
      data() {
        return {
          setting: {
            title: "",
            keywords: "",
            description: "",
          },
        };
      },
      metaInfo() {
        return {
          title: this.setting.title,
          meta: [
            { name: "keywords", content: this.setting.keywords },
            { name: "description", content: this.setting.description },
          ],
        };
      },
      mounted() {
        this.Init()
      },
      methods: {
        Init(){
          // 模拟接口获取数据
          this.setting.title = "页面标题1";
          this.setting.keywords = "页面关键字1";
          this.setting.description = "页面描述1";
        }
      }
    }
</script>
<style scoped>
</style>
设置好路由之后,访问页面,查看head部分

本文参考链接:
https://www.freesion.com/article/18001091411/
上一篇: vue 404页面
 51250
 50685
 41286
 38107
 32571
 29471
 28333
 23196
 23160
 21488
 1564°
 2284°
 1890°
 1829°
 2142°
 1876°
 2565°
 4298°
 4150°
 2959°