2021-vue项目SEO优化->vue-meta插件动态设置meta和title标签

- 日理万妓 2022-10-30 14:23 74阅读 0赞

2021-vue项目SEO优化->vue-meta插件动态设置meta和title标签

前言

  • 不想用了ssr 技术,我就是想要优化,这个方法是最好的
  • 如何去使用md来写文章

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

一、meta标签是什么

  1. 数据(metadata)是关于数据的信息。
  2. 标签提供关于 HTML 文档的元数据。元数据不会显示在页面上,但是对于机器是可读的。
  3. 典型的情况是,meta 元素被用于规定页面的描述、关键词、文档的作者、最后修改时间以及其他元数据。
  4. 标签始终位于 head 元素中。
  5. 元数据可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 web 服务。
  6. meta标签可以用来描述一个HTML网页文档的属性,例如作者、日期和时间、网页描述、关键词、页面刷新等。
  7. seo优化中,合理利用 Meta 标签的 Description Keywords属性,加入网站的描述或者网页的关键字,可使网站更加贴近用户体验。
  8. 今天我们就要使用的就是关于Meta 标签的 Description Keywords属性

二、静态设置meta标签属性

①、首先下载相关包

  1. npm install vue-meta --save yarn add vue-meta

②、在main.js中全局使用

  1. import Meta from 'vue-meta';
  2. // 使用vue-meta
  3. Vue.use(Meta);

③、给每个route赋一个静态属性对象

  1. const routes = [
  2. {
  3. path: '/',
  4. name: 'website_index',
  5. component: website_index,
  6. children: [
  7. // 官网首页
  8. {
  9. path: '/',
  10. name: '/',
  11. component: home_main,
  12. meta: {
  13. metaInfo: {
  14. title: "首页",
  15. keywords: "资讯,新闻,财经,房产,视频,NBA,科技",
  16. description: "我司从2003年创立至今,已经成为集新闻信息……"
  17. }
  18. }
  19. }, // 解决方案详细信息
  20. {
  21. path: 'solutions_detail',
  22. name: 'solutions_detail',
  23. component: solutions_detail,
  24. meta: {
  25. metaInfo: {
  26. title: "解决方案",
  27. keywords: "资讯,新闻,财经,房产,视频,NBA,科技",
  28. description: "我司从2003年创立至今,已经成为集新闻信息……"
  29. }
  30. }
  31. },
  32. // 关于我们
  33. {
  34. path: 'about_hc',
  35. name: 'about_hc',
  36. component: about_hc,
  37. meta: {
  38. metaInfo: {
  39. title: "关于我们",
  40. keywords: "资讯,新闻,财经,房产,视频,NBA,科技",
  41. description: "我司从2003年创立至今,已经成为集新闻信息……"
  42. }
  43. }
  44. }
  45. ]
  46. },
  47. ]

④、在vuex中存储一个空的属性对象和定义方法

  1. export default new Vuex.Store({
  2. state: {
  3. baseUrl:'http://192.168.31.39:8060/',
  4. // 默认网站关键词
  5. metaInfo: { }
  6. },
  7. mutations: {
  8. CAHNGE_META_INFO(state, metaInfo) {
  9. console.log(metaInfo,"metaInfo")
  10. state.metaInfo = metaInfo;
  11. }
  12. },
  13. actions: {
  14. },
  15. modules: {
  16. }
  17. })

⑤、最后在main.js中使用路由拦截守卫

  1. router.beforeEach((to, from, next) => {
  2. if (to.meta.metaInfo){
  3. store.commit("CAHNGE_META_INFO", to.meta.metaInfo)
  4. }
  5. next()
  6. });
  7. new Vue({
  8. router,
  9. store,
  10. metaInfo(){
  11. return {
  12. title: this.$store.state.metaInfo.title,
  13. meta: [
  14. {
  15. name: "keywords",
  16. content: this.$store.state.metaInfo.keywords
  17. }, {
  18. name: "description",
  19. content: this.$store.state.metaInfo.description
  20. }
  21. ]
  22. }
  23. },
  24. render: h => h(App)
  25. }).$mount('#app')

三、动态设置meta标签属性

设置动态的meta标签属性的话,我们可以在静态设置的基础上修改。比如修改某个路由的动态meta标签属性

①、修改单个路由

  1. const routes = [
  2. {
  3. path: '/',
  4. name: 'website_index',
  5. component: website_index,
  6. children: [
  7. // 官网首页
  8. {
  9. path: '/',
  10. name: '/',
  11. component: home_main,
  12. meta: {
  13. metaInfo: {
  14. title: "首页",
  15. keywords: "资讯,新闻,财经,房产,视频,NBA,科技",
  16. description: "我司从2003年创立至今,已经成为集新闻信息……"
  17. }
  18. }
  19. },
  20. // 解决方案详细信息
  21. {
  22. path: 'solutions_detail',
  23. name: 'solutions_detail',
  24. component: solutions_detail,
  25. meta: {
  26. metaInfo: {
  27. title: "解决方案",
  28. keywords: "资讯,新闻,财经,房产,视频,NBA,科技",
  29. description: "我司从2003年创立至今,已经成为集新闻信息……"
  30. }
  31. }
  32. },
  33. // 关于我们
  34. {
  35. path: 'about_hc',
  36. name: 'about_hc',
  37. component: about_hc
  38. }
  39. ]
  40. },
  41. ]

删除关于我们的路由静态的meta属性

②、动态赋值

用在该路由页面加载时获取到的动态属性赋值给vuex中的对象属性

  1. mounted () {
  2. // 假设这是我们获取到的动态数据
  3. let metaInfo = {
  4. title: "张先生",
  5. keywords: "玉树临风,风流倜傥",
  6. description: "前方路过村庄,记得带一箱土鸡蛋回去~"
  7. }
  8. this.$store.commit("CAHNGE_META_INFO", metaInfo)
  9. }

发表评论

表情:
评论列表 (有 0 条评论,74人围观)

还没有评论,来说两句吧...

相关阅读