码迷,mamicode.com
首页 > 其他好文 > 详细

vue根据路由动态修改页面标题

时间:2020-10-21 20:22:18      阅读:26      评论:0      收藏:0      [点我收藏+]

标签:routes   code   根据   标题   meta   port   component   vue   变化   

router.js 参考如下

Vue.use(VueRouter);

const routes = [
    {
        path: "/",
        component: index,
        name: "index",
        meta: {
            title: ‘首页‘
        }
    }]
var router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    next();
})
export default router;

前提条件

确保vue项目的index.html文件中有

<title><%= htmlWebpackPlugin.options.title %></title>

默认vue cli是有这行代码的

补充

你也可以直接在你的业务页面修改标题

//在业务页面修改标题
document.title = "标题名称";

vue根据路由动态修改页面标题

标签:routes   code   根据   标题   meta   port   component   vue   变化   

原文地址:https://www.cnblogs.com/creci/p/13848568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!