vue是单页面应用,我们怎么给每个页面设置单独的标题呢?
1.首先在路由文件中添加meta属性,meta中设置title
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import home from "@/components/Home.vue"
import news from "@/components/News.vue"
export default new Router({
routes:[
{
path:'/',
name:'home',
meta:{title:'首页'},
component:home
},
{
path:'/news',
name:'news',
meta:{title:'新闻页'},
component:news
},
{
path:'*',
redirect:'/'
}
]
})
import Router from 'vue-router'
Vue.use(Router)
import home from "@/components/Home.vue"
import news from "@/components/News.vue"
export default new Router({
routes:[
{
path:'/',
name:'home',
meta:{title:'首页'},
component:home
},
{
path:'/news',
name:'news',
meta:{title:'新闻页'},
component:news
},
{
path:'*',
redirect:'/'
}
]
})
2.然后在main.js中,遍历路由,给每个页面设置标题
在main.js中添加这段代码就可以