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 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?