在Vue組件化開發過程中,因為是單頁面開發,但是有時候需要頁面的title根據情況改變,於是上網查了一下,各種說法花(wo)里(kan)胡(bu)哨(dong), 於是想到一個黑科技 documet.title="xxx";
隨便創建一個項目,在獨立的模塊中,created在鈎子函數啟動之后document.title='標題'; 但是據說在IOS的微信下是無效的,雖然用蘋果機測試過有用,但是想到這樣會影響我的代碼潔癖。
<script> export default { data(){ return{ } }, created(){ document.title="首頁" }, } </script>
於是在github上找到一個好用的東西 vue-wechat-title
通過 npm install vue-wechat-title 安裝
引入需要的vue-router與頁面需要的組件之后
const router = new VueRouter({
mode: 'history',
routes:[
{
name: 'index',
path: '/',
meta: {
title: '首頁'
},
component: index
},
{
name: 'root',
path: '/root',
meta: {
title: '肉特'
},
component: root
}
]
});
Vue.use(require('vue-wechat-title')); //實例化參數
在組件中頂部添加一段 <div v-wechat-title="$route.meta.title"></div>
即可實現改變title效果。
