Vue设置动态title,刷新页面后title设置失效

1. 在 router/index.js 中,使用meta属性的title设置好每个路由对应的title值

/* router/index.js: */
 
const routes = [
 {
    path: '/home/search',
    name: 'search',
    component: search,
	meta: {
		title: '首页'
	}
  }]

2.在 main.js 中设置路由守卫,vue页面切换时,网页title随之切换

/* main.js: */
 
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

设置好之后,路由就生效了,但是很多兄弟遇到了这么一个问题:刷新页面之后title失效了

原因

查询 vue-router1.0 的说明文档,文档上的 Basic Usage 代码如下:

// Load the plugin
Vue.use(VueRouter)

// Define some components
var Foo = {
    template: '<p>This is foo!</p>'
}

var Bar = {
    template: '<p>This is bar!</p>'
}

// The router needs a root component to render.
// For demo purposes, we will just use an empty one
// because we are using the HTML as the app template.
// !! Note that the App is not a Vue instance.
var App = {}

// Create a router instance.
// You can pass in additional options here, but let's
// keep it simple for now.
var router = new VueRouter()

// Define some routes.
// Each route should map to a component. The "component" can
// either be an actual component constructor created via
// Vue.extend(), or just a component options object.
// We'll talk about nested routes later.
router.map({
    '/foo': {
        component: Foo
    },
    '/bar': {
        component: Bar
    }
})

// Now we can start the app!
// The router will create an instance of App and mount to
// the element matching the selector #app.
router.start(App, '#app')

可以发现,样例中把 router.start(App, '#app') 这行代码放在了最后。因为这一步是创建和挂载根实例,是启动路由的最后一步,需要在定义路由实例和配置路由之后进行。

 

解决:

在使用 vue-router 模块时,挂载根实例的步骤要放在最后,不然会导致配置不成功。

也就是第二步

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

-------这里一定要放在最后面就可以了-------------

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

或者

router.start(App, '#app')

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当使用vuex持久化插件时,刷新页面动态路由会丢失。这是因为在刷新页面时,路由不是响应式的,导致找不到之前添加的动态路由而出现空白页面。解决这个问题的方法是在main.js的new Vue()加入created()钩子函数,并将创建动态路由的方法放在其。这样在刷新页面时,会重新执行创建动态路由的方法,解决了动态路由丢失的问题。另外,还可以通过在router.beforeEach()进行拦截来解决刷新页面动态路由丢失的问题。具体的代码实现可以参考引用\[2\]的示例代码。 #### 引用[.reference_title] - *1* [vue2动态路由刷新页面空白](https://blog.csdn.net/weixin_42821697/article/details/126421090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue动态添加路由后刷新失效问题](https://blog.csdn.net/weixin_48309048/article/details/128250397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [关于Vue刷新后丢失动态路由](https://blog.csdn.net/weixin_42652039/article/details/127957451)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值