动态修改vue-cli的spa的标题

喜欢ヅ旅行 2022-05-29 07:07 243阅读 0赞
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Home from '@/components/home'
  4. import Items from '@/components/Items'
  5. import Waterfull from '@/components/Waterfull'
  6. Vue.use(Router)
  7. const router = new Router({
  8. mode: 'history',
  9. routes: [
  10. {
  11. path: '/',
  12. name: 'home',
  13. alias: '/home',
  14. meta: {
  15. title: '首页'
  16. },
  17. component: Home
  18. },
  19. {
  20. path: '/items',
  21. name: 'Items',
  22. meta: {
  23. title: 'web项目'
  24. },
  25. component: Items
  26. },
  27. {
  28. path: '/items/Waterfull',
  29. name: 'Waterfull',
  30. meta: {
  31. title: '瀑布流'
  32. },
  33. component: Waterfull
  34. }
  35. ]
  36. })
  37. router.beforeEach(function(to,from,next){
  38. if(to.meta.title){
  39. document.title = to.meta.title
  40. }else{
  41. document.title = 'personal learn'
  42. }
  43. next()
  44. })
  45. export default router

主要通过

  1. router.beforeEach(function(to,from,next){
  2. if(to.meta.title){
  3. document.title = to.meta.title
  4. }else{
  5. document.title = 'personal learn'
  6. }
  7. next()
  8. })

进行修改;

发表评论

表情:
评论列表 (有 0 条评论,243人围观)

还没有评论,来说两句吧...

相关阅读