首页 > 解决方案 > 无法重新定义属性:$router,可能重复 Vue.use(VueRouter)

问题描述

我的 Vue 应用程序中有几条路线。但是,刷新页面只会在特定的 2 条路线中导致永无止境的循环。我从 chrome 控制台收到的错误是

VM10283:55826 Uncaught TypeError: Cannot redefine property: $router
    at Function.defineProperty (<anonymous>)
    at Function.install (<anonymous>:55826:10)
    at Function.Vue.use (app.js:62595)
    at Module../node_modules/vue-router/dist/vue-router.esm.js (<anonymous>:57490:14)
    at __webpack_require__ (<anonymous>:20:30)
    at Module../resources/js/app.js (<anonymous>:71691:68)
    at __webpack_require__ (<anonymous>:20:30)
    at Object.0 (<anonymous>:74685:1)
    at __webpack_require__ (<anonymous>:20:30)
    at <anonymous>:84:18

以下是我的代码

应用程序.js

import VueRouter from 'vue-router'
import routes from '@/js/router'
Vue.use(VueRouter);
const router = new VueRouter({
    mode: 'history',
    hash: false,
    routes
});

const app = new Vue({
    el: '#app',
    store,
    router,
    components: { App },
    template: '<App/>',
});

路由器/index.js

import Home from '@/js/components/Home.vue';
import store from '@/js/store'

export default [
    {
        path: '/',
        name: 'home',
        beforeEnter: publicAuth,
        component: Home,
        meta: {
            title: 'Home',
        }
    },
    {
        path: '/user/profile',
        name: 'user.profile',
        beforeEnter: auth,
        component: Profile,
        meta: {
            title: 'Profile',
        }
    },
...

在 SO 上搜索后,我意识到这可能是由 引起的Vue.use(VueRouter),我已经尝试了网络上的所有方法,但没有找到解决方案。

谢谢

标签: javascriptvue.js

解决方案


推荐阅读