前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue meta设置页面

vue meta设置页面

作者头像
py3study
发布2021-03-23 21:08:21
3.3K0
发布2021-03-23 21:08:21
举报
文章被收录于专栏:python3python3

一、概述

在项目中,有一个搜索页面,需要根据不同的关键字,动态修改meta信息,用于seo优化。

本文接下来介绍如何使用 vue-meta 修改页面头部信息

二、vue-meta使用

安装

代码语言:javascript
复制
npm?install?--save?vue-meta

main.js使用依赖

修改main.js,增加2行代码

代码语言:javascript
复制
//?使用?vue-meta
import?Meta?from?"vue-meta";
Vue.use(Meta);

固定的meta

test.vue

代码语言:javascript
复制
<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部分

1.png
1.png

可以发现,对应的值,已经修改了。?

动态的meta

根据请求数据,设置meta

代码语言:javascript
复制
<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部分

1.png
1.png

本文参考链接:

https://www.freesion.com/article/18001091411/

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-03-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、概述
  • 二、vue-meta使用
    • 安装
      • main.js使用依赖
        • 固定的meta
          • 动态的meta
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com