vue实现根据页面动态改变title
老板,来一盘代码
效果:
index.js 中
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
mode:"history",
routes: [
{
path: '/',
name: 'one',
component: ()=>import("@/components/one"),
meta:{
title: '首页',
}
},
{
path: '/two',
name: 'two',
component: ()=>import("@/components/two"),
meta:{
title: '关于我们',
}
},
]
})
main.js 中
import Vue from 'vue'
import App from './App'
import router from './router'
router.beforeEach((to, from, next) => {
/* 路由发生变化修改页面title */
if (to.meta.title) {
document.title = 'swx-'+to.meta.title
} else {
document.title = 'swx'
}
next()
});
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
还没有评论,来说两句吧...