vue动态设置title:history.replaceState

938 阅读1分钟

h5vue页面,在请求接口之后,动态修改title,但是在设置document.title = res.data.title页面并没有发生变化,但是html中的title变了

title是通过router设置的

router.beforeEach( ( to, from, next ) => {
    const title = to.meta && to.meta.title
    if ( title ) {
        document.title = title
    } 
    next()
} );

解决:

document.title = res.data.title
history.replaceState(null,'')

利用HTML5的history.replacestate()修改当前页面的参数

history.replaceState(stateObj, title, [url])