html设置重置title,动态设置html的title

使用vue前端框架做,竟然丢弃了很多javascript和html的东西了。。 动态设置title的方法: 1.使用vue的自定义指令 {{htmltitle

使用vue前端框架做,竟然丢弃了很多javascript和html的东西了。。

动态设置title的方法:

1.使用vue的自定义指令

{{htmltitle}}

...

directives: {

title: {

inserted: function (el, binding) {

document.title = el.innerText

el.remove()

}

}

}

2.很简单

// 设置html title

document.title = sessionStorage.getItem('title')

3.router路由不同的title

...

routes: [

{

path: '/index',

name: 'index',

component: index,

meta:{

title:'首页'

}

},

{

path: '/user',

name: 'user',

component: user,

meta:{

title:'个人中心'

}

}

]

...

router.beforeEach((to, from, next) => {

if (to.meta.title) {

document.title = to.meta.title

}

next()

})