3502 2021-03-18 17:58 采纳率: 50%
浏览 1122
已采纳

Vue报错beforeEach is not a function

import Vue from 'vue'
import Router from 'vue-router'
/* import User from '../components/User'
import Home from '../components/Home'
import About from '../components/About' */
import VueRouter from 'vue-router'


const Home =() => import ('../components/Home')
const About =() => import ('../components/About')
const User =() => import ('../components/User')
const HomeNew =() => import ('../components/HomeNews')
const Homemessage =() => import ('../components/Homemessage')

Vue.use(Router)

export default new Router({
 
  mode: 'history',
  linkActiveClass:'active',

  routes: [
    {
      path:'',
      //redirect重定向
      redirect:'/home'
    },
    {
      path: '/home',
      component: Home,
      
      meta:{
        title:'首页'
      },
      children:[
        {
          path:'',
          //redirect重定向
          redirect:'news'
        },
        {
          path:'news',
          component:HomeNew
        },{
          path:'message',
          component:Homemessage
        },
      ]
    },
    {
      path: '/about',
      component:About,
      meta:{
        title:'关于'
      },
    },
    
    {
      path:'/user/:userId',
      component:User,
      meta:{
        title:'用户'
      },
    }
  ]
 
})
Router.beforeEach((to,from,next) => {
  //从from跳转到to
  document.title = to.matched[0].meta.title
  console.log(to);
  next()
})

刚接触vue路由不明白是哪里出现问题,求大佬指点

  • 写回答

5条回答 默认 最新

  • 流转的年华 2021-03-19 10:01
    关注

    你这个router定义的时候是匿名的,所以使用的时候找不到,你可以这样改:

    const routes = [
     //界面路由
    ]
    const router = new VueRouter({
      mode:'history',
        linkActiveClass:'active',
    
       routes })
    
    router.beforeEach((to, from, next) => {
     //业务逻辑
    }
    export default router
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题