1. Speichern Sie zunächst den anzuzeigenden Titel lokal.
if (that.PromotionInfo.Title) {
localStorage.setItem("AcTitle", that.PromotionInfo.Title)
}
2. Legen Sie nun den Titel im Routing-Meta fest und legen Sie ihn dann im Routing-Guard fest.
import Vue from 'vue'
import Router from 'vue-router'
import promptDetail from './views/promptDetail'
Vue.use(Router)
const router = new Router({
routes: [
{
//活动详情
path: '/',
name: 'promptDetail',
meta:{title:localStorage.getItem("AcTitle")},
component: promptDetail
},
],
})
router.beforeEach((to,from,next)=>{//beforeEach是router的钩子函数,在进入路由前执行
if(to.meta.title){//判断是否有标题
document.title = to.meta.title
}
next() //执行进入路由,如果不写就不会进入目标页
})
export default router
Oh, kaufen Sie es ~ Es ist lokal möglich, aber nicht in der Produktionsumgebung.
Hier ist die Lösung.
if (that.PromotionInfo.Title) {
// localStorage.setItem("AcTitle", that.PromotionInfo.Title)
console.log("页面设置",document.title);
document.title = that.PromotionInfo.Title;
console.log("页面设置之后",document.title);
}