【问题标题】:How I use vue-router with vue 2.x versions我如何在 vue 2.x 版本中使用 vue-router
【发布时间】:2021-06-07 01:58:27
【问题描述】:

从 'vue' 导入 Vue 从 'vuex' 导入 Vuex 从 'vue-router' 导入 VueRouter 导入'es6-promise/auto'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'

//import About from "./components/About.vue"

Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(Vuex)
Vue.use(VueRouter)

const store = new Vuex.Store({ ... })

const routes = [
  { path: '/about', component: { template: '<div>foo</div>' } },
]

const router = VueRouter({
  routes, // short for `routes: routes`
})

new Vue({
  render: h => h(App),
  router,
  store,
}).$mount('#app')

我尝试使用路由器,但出现以下错误:

Uncaught TypeError: this is undefined
    VueRouter vue-router.esm.js:2699
    <anonymous> main.js:84
    js app.js:1351
    __webpack_require__ app.js:849
    fn app.js:151
    1 app.js:1364
    __webpack_require__ app.js:849
    checkDeferredModules app.js:46
    <anonymous> app.js:925
    <anonymous> app.js:928
vue-router.esm.js:2699
    VueRouter vue-router.esm.js:2699
    <anonim> main.js:84
    js app.js:1351
    __webpack_require__ app.js:849
    fn app.js:151
    1 app.js:1364
    __webpack_require__ app.js:849
    checkDeferredModules app.js:46
    <anonim> app.js:925
    <anonim> app.js:928

我创建了我的项目 vue cli,然后我使用 npm -i 安装了 vue-router,为什么它不能以这种方式工作,或者我应该使用vue add router cli 命令添加?

这是我的版本:

    "vue": "^2.6.12",
    "vue-router": "^3.2.0",
    "vuex": "^3.6.2"
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "^4.5.11",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "babel-plugin-component": "^1.1.1",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "vue-template-compiler": "^2.6.11"

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:

    该错误不是VueRouter导入错误而是VueRouter使用错误。

    VueRouter 函数是获取路由器实例的构造函数。在您的代码中,您必须这样做才能解决您的问题:

    const router = new VueRouter({
      routes, // short for `routes: routes`
    })
    

    【讨论】:

    • VueRouter 功能不工作。我记录并调用它,它给出了同样的错误
    猜你喜欢
    • 2017-05-29
    • 2017-02-15
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多