seo vue 动态路由_基于vue.jsvue-router的动态更新TDK(SEO优化)

本文基于工作项目开发,做的整理笔记

前几天帮朋友解决这个问题,顺带学习了一下,做个笔记Mark下。

前提条件:

你已经了解并使用vue,能够搭建应用站点。

编码环境:

system:OS X EI Capitan 10.13.3

npm:5.5.1

node:v8.9.3

vue-cli:@lastest

相关技术栈:

vue2 + vue-router

基于vue.js&vue-router的动态更新TDK.jpg

1.router部分

直接看代码,主要包含一个配置变量mapping,也可以抽出去放到单独的配置文件中,这里就简化放进来了,容易看。在路由配置中,给了一个meta字段用于存放对应的TDK信息。

注意:这里我们发现NewsDetail暂存的TDK信息和News一样,没关系,因为它和ID有关,在app.js那边我们还要具体处理。

/**********************************************/

/* src/router/index.js */

/**********************************************/

import Vue from 'vue'

import Router from 'vue-router'

import Layout from '@/components/layout'

import Home from '@/components/home'

import News from '@/components/hot-news'

import Detail from '@/components/hot-news/detail'

import About from '@/components/info/about'

import ContactUs from '@/components/info/contact-us'

import JoinUs from '@/components/info/join-us'

import Terms from '@/components/info/terms'

import Error404 from '@/components/404'

Vue.use(Router)

const mapping = {

home: {

title: 'xxxxxxxxxxxxxxxx-【官网名称】',

metaTags: [

{

name: 'keywords',

content: 'xxx, xx, xxxxx, xxx, xxx'

},

{

name: 'description',

content: '官网页面的一段描述'

},

],

},

news: {

title: '【新闻热点】_xxxxxxx-官网名称',

metaTags: [

{

name: 'keywords',

content: 'xxx, xx, xxxxx, xxx, xxx'

},

{

name: 'description',

content: '新闻热点页面的一段描述'

},

],

},

}

export default new Router({

mode: 'history',

routes: [

{

path: '/',

component: Layout,

// redirect: '/home',

hidden: true,

children: [

{

path: '',

component: Home,

meta: mapping.home,

},

{

path: 'home',

redirect: '/',

component: Home,

meta: mapping.home,

},

{

path: 'news',

component: News,

meta: mapping.news,

},

{

path: 'news/:id',

component: NewsDetail,

meta: mapping.news,

},

{

path: 'about',

component: About,

meta: mapping.home,

},

{

path: 'contact-us',

component: ContactUs,

meta: mapping.home,

},

{

path: 'join-us',

component: JoinUs,

meta: mapping.home,

},

{

path: 'terms',

component: Terms,

meta: mapping.home,

},

{

path: '404',

component: Error404,

meta: mapping.home,

},

]

},

]

})

2. 看看app.js这边如何使用

这段代码中,也有一个配置变量mappingFull,它主要服务于那种带查询条件的,带id的具体路由的TDK。另外注意的就是方法

/**********************************************/

/* src/app.js */

/**********************************************/

// The Vue build version to load with the `import` command

// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'

import App from './App'

import store from './store';

import router from './router'

import ElementUI from 'element-ui';

import 'element-ui/lib/theme-chalk/index.css';

import '@/style/base.scss'

Vue.use(ElementUI);

Vue.config.productionTip = false

const mappingFull = {

'/news?categary=xxxxx': {

title: '【新闻热点】_xxxxx-官网名称',

metaTags: [

{

name: 'keywords',

content: 'xxx, xx, xxxx, xxx, xxx'

},

{

name: 'description',

content: '这一搜索条件的相关描述'

},

],

},

'/news/1': {

title: '【xxxxx_新闻热点】_xxxxxx-官网名称',

metaTags: [

{

name: 'keywords',

content: 'xxx, xx, xxxx, xxx, xxx'

},

{

name: 'description',

content: '新闻热点1的相关描述'

},

],

},

'/news/2': {

title: '【xxxxx_新闻热点】_xxxxxx-官网名称',

metaTags: [

{

name: 'keywords',

content: 'xxx, xx, xxxx, xxx, xxx'

},

{

name: 'description',

content: '新闻热点2的相关描述'

},

],

},

'/hot-news/3': {

title: '【xxxxx_新闻热点】_xxxxxx-官网名称',

metaTags: [

{

name: 'keywords',

content: 'xxx, xx, xxxx, xxx, xxx'

},

{

name: 'description',

content: '新闻热点3的相关描述'

},

],

},

}

router.beforeEach((to, from, next) => {

const matchPath = to.matched.slice().reverse();

const meta = mappingFull[to.fullPath];

// console.log(to.fullPath); // 可以打印输出看看

// console.log(meta);

let nearestWithTitle = undefined;

let nearestWithMeta = undefined;

if (meta) {

nearestWithTitle = { meta };

nearestWithMeta = { meta };

} else {

nearestWithTitle = matchPath.find(r => r.meta && r.meta.title);

nearestWithMeta = matchPath.find(r => r.meta && r.meta.metaTags);

}

if(nearestWithTitle) document.title = nearestWithTitle.meta.title;

Array.from(document.querySelectorAll('[data-vue-router-controlled]')).map(el => el.parentNode.removeChild(el));

if(!nearestWithMeta) return next();

nearestWithMeta.meta.metaTags.map(tagDef => {

const tag = document.createElement('meta');

Object.keys(tagDef).forEach(key => {

tag.setAttribute(key, tagDef[key]);

});

tag.setAttribute('data-vue-router-controlled', '');

return tag;

})

.forEach(tag => document.head.appendChild(tag));

if (to.need && to.need.requireAuth) {

if (store.getters.token) {

next();

}

else {

next('/home');

// next({

// path: '/login',

// query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由

// });

}

}

else {

next();

}

});

router.afterEach((to,from,next) => {

window.scrollTo(0,0);

})

new Vue({

store,

router,

render: h => h(App)

}).$mount('#app');

3. 疑问

我们大多都知道SEO优化有很多方式,更可行的可能是SSR后端渲染,但是毕竟有时候我们就是前端渲染实现某个官网,只能找找其他简单可行办法。文中这种做法实际上只有真是访问的时候才可以拿到真正准确的TDK,各搜索引擎的爬虫拿到的是静态文件index.html的代码view-source,那个时候TDK还没有被替换,不知道这种做法是否真的有利于SEO。有待验证。

欢迎留言提出此类问题的优化,互相学习。

学习是一条漫漫长路,每天不求一大步,进步一点点就是好的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440593.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

*多叉树的树形背包常见建模方法

一.多叉树变二叉树。 这个技巧其实也有两种具体的方法:树的孩子兄弟表示法与dfs序法。 1.树的孩子兄弟表示法。 大家在学习树形结构时一定接触了一个多叉树变二叉树的方法,就是把每个点与它的第一个儿子连边,然后将它的儿子依次连接起来。可…

python把英语句子成分字母_求一个可以分析英语句子成分的软件或网站

目前还没有,最好的翻译软件都不能翻译英语语法,只能翻译语法结构简单的句子,更别提专业分析句子成分的软件了;出现专业分析英语句子成分的软件,英语老师应该就失业了。求一个可以分析英语句子成分的软件或网站句子是按…

【51nod - 1076】2条不相交的路径(Tarjan无向图判环)

题干: 给出一个无向图G的顶点V和边E。进行Q次查询,查询从G的某个顶点V[s]到另一个顶点V[t],是否存在2条不相交的路径。(两条路径不经过相同的边) (注,无向图中不存在重边,也就是说…

python 导入csv文件到oracle_python将文件夹下的所有csv文件存入mysql和oracle数据库

#oracle版首先新建python脚本(d:/python/orclImport.py)import os #引入os包if __name__ __main__:#遍历文件夹下所有的dmp文件,其中filename为所有文件、文件夹的名称。#因为我文件夹下确定都是dmp文件,所以无需进行特殊判断for filename in os.listdi…

createform用法_vue自定义表单生成器form-create使用详解

介绍form-create 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的表单生成器。并且支持生成任何 Vue 组件。结合内置17种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定。文档 | github演示项目: 开源的高品质微信商城功能自定义组件可生…

【蓝桥杯官网试题 - 历届试题】发现环(dfs+并查集,或无向图tarjan判环,无向环,或拓扑排序)

题干: 问题描述 小明的实验室有N台电脑,编号1~N。原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络。在树形网络上,任意两台电脑之间有唯一的路径相连。   不过在最近一次维护网络时,管理员误操作使得某…

倍福 在 vs 里 编程 是怎么做到的_截图里的文字要改,字体怎么做到一模一样?...

大家是不是碰到过这么一个尴尬的情况:一个截图里有一个错别字,或者其中一小段文字要修改,可是苦于找不到源文件,又没办法找出和原来文字一模一样的字体。每次遇到这样的情况你都是怎么处理的呢?火箭君今天给大家介绍一…

【ZOJ - 3591】Nim(博弈问题,思维,STLmap)

题干: Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. The game ends when one of the players is unable to remove object in his/her turn. This player will then lose. On each turn, a pla…

【牛客 - 369B】小A与任务(贪心,优先队列)

题干: 链接:https://ac.nowcoder.com/acm/contest/369/B 来源:牛客网 小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务 第 i 个任务需要花费 xixi 的时间…

word硬回车是怎么产生的_在word中怎样删除软硬回车?

1.点击“开始”-“程序”-“附件”-“记事本”,打开“记事本”,放在一边。2.打开需要调整的Word文档,在文章的任意处单击一下鼠标左键。然后按住键盘的“Ctrl”键不要松,再按一下“A”键&#x…

*【POJ - 3659】Cell Phone Network (树形dp,最小支配集)

题干: Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so the…

测试jdbc连mysql数据库_java连接mysql数据库及测试是否连接成功的方法

本文实例讲述了java连接mysql数据库及测试是否连接成功的方法。分享给大家供大家参考,具体如下:package com.test.tool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import…

mysql配置日志老化配置_mysql配置-日志大小限制和自动删除

线上的项目磁盘消耗问题, 发现和MySQL日志有关系.需要处理的问题如何限制大小 不让日志无限膨胀?配置日志不留?删除的方式和直接删除会对服务有什么影响?解决方式限制大小, 保留最近一段时间日志.set global expire_logs_days7; # 命令行进入MySQL中, 临时设置保留最近7天日…

【HDU - 5091】Beam Cannon(线段树,扫描线)

题干: Recently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on t…

mysql的传播特性_spring事务传播特性和mysql事务隔离级别

spring事务的传播特性--7种REQUIRED支持当前事务,如果没有事务会创建一个新的事务SUPPORTS支持当前事务,如果没有事务的话以非事务方式执行MANDATORY(强制性)支持当前事务,如果没有事务抛出异常REQUIRES_NEW创建一个新的事物并挂起当前事务NO…

【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)

题干: Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently. Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both si…

【HDU - 5090】Game with Pearls (匈牙利算法,二分图匹配)

题干: Tom and Jerry are playing a game with tubes and pearls. The rule of the game is: 1) Tom and Jerry come up together with a number K. 2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube i…

qt同时连接oracle和mysql_QT连接Oracle和Mysql的详细步骤,已成功~!

近几天一直在整QT的数据库连接这一块。因为QT是开源的,所以涉及的连接Oracle及Mysql的驱动都必须自己编译生成。通过不断的测试、调试,终于把QT连接Oracle和Mysql的驱动编译生成好了。QT环境:Qt 4.6.0打开Qt Command Prompt,开始菜…

【POJ - 2632】Crashing Robots(模拟)

题干: In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occup…

一台linux上运行多个mysql_linux下同时运行多个mysql

来自网络,感谢开源,感谢分享通过rpm安装mysql,测试版本5.1.481、在linux下通过:#useradd multi -g mysql -s /sbin/nologin添加一个multi用户,并加入到mysql组#passwd multi给multi用户添加密码,这里设置的密码为multi…