使用路由器文件时无法读取未定义的匹配项
Cannot read matched of undefined when using router file
所以我有一个简单的路由器文件,它为我网站的不同部分导入不同的路由,所以:
general.js(我的一个路由文件)
import Home from '../../components/home/home'
const routes = [{
name: 'home',
path: '/',
component: Home
}]
export default routes
router.js
import Vue from 'vue'
import Router from 'vue-router'
import GeneralRoutes from './general'
Vue.use(Router)
const routes = Array.prototype.concat(
GeneralRoutes
)
const router = new Router({
mode: 'history',
routes
})
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth) {
next({replace: true, name: 'login'})
}
next()
})
export default router
那是我的路由器,现在在我的 app.js 中,我只是这样使用它:
import Vue from 'vue'
import Router from './core/routes/router'
new Vue({
el: '#app',
data: () => ({
user: ''
}),
Router,
});
但是这是抛出一个错误,说明如下:
[Vue warn]: Error in render: "TypeError: Cannot read property
'matched' of undefined"
当我 console.log
路由器时,我得到以下信息:
router
是小写
import Vue from 'vue'
import Router from './core/routes/router'
new Vue({
el: '#app',
data: () => ({
user: ''
}),
router: Router
});
所以我有一个简单的路由器文件,它为我网站的不同部分导入不同的路由,所以:
general.js(我的一个路由文件)
import Home from '../../components/home/home'
const routes = [{
name: 'home',
path: '/',
component: Home
}]
export default routes
router.js
import Vue from 'vue'
import Router from 'vue-router'
import GeneralRoutes from './general'
Vue.use(Router)
const routes = Array.prototype.concat(
GeneralRoutes
)
const router = new Router({
mode: 'history',
routes
})
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth) {
next({replace: true, name: 'login'})
}
next()
})
export default router
那是我的路由器,现在在我的 app.js 中,我只是这样使用它:
import Vue from 'vue'
import Router from './core/routes/router'
new Vue({
el: '#app',
data: () => ({
user: ''
}),
Router,
});
但是这是抛出一个错误,说明如下:
[Vue warn]: Error in render: "TypeError: Cannot read property 'matched' of undefined"
当我 console.log
路由器时,我得到以下信息:
router
是小写
import Vue from 'vue'
import Router from './core/routes/router'
new Vue({
el: '#app',
data: () => ({
user: ''
}),
router: Router
});