简体   繁体   English

Vue.js 3 - 为什么在这种情况下导入 vue-router 不起作用?

[英]Vue.js 3 - Why is importing vue-router not working in this case?

I have the following files我有以下文件

router.js路由器.js

import VueRouter from 'vue-router'

export const router = VueRouter({
routes: [
     {
      ...
     }
 ]
})

main.js main.js

import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
...
import { router } from './router'
app.use(router)
...
app.mount('#app')

However when doing this I get the following error Uncaught TypeError: this is undefined - vue-router.esm.js:2828 it seems to be an error inside vue-router library since the error occures at line 2828.但是,当这样做时,我收到以下错误 Uncaught TypeError: this is undefined - vue-router.esm.js:2828它似乎是 vue-router 库中的一个错误,因为错误发生在第 2828 行。

This happens by just trying to import vue-router without trying to use it anywhere else in the app, I also use vuex and import export/import store the same way and it works.这是通过尝试导入 vue-router 而不尝试在应用程序中的其他任何地方使用它来实现的,我也以相同的方式使用 vuex 和导入导出/导入存储并且它可以工作。

My guess is that I'm importing vue-router wrong because the docs don't use {createApp} in the example.我的猜测是我错误地导入了 vue-router,因为文档在示例中没有使用{createApp} Am I importing it wrong, or is there some other reason it doesn't work?是我导入错了,还是有其他原因它不起作用?

First make sure the you've installed the vue router 4 using:首先确保您已使用以下命令安装了 vue 路由器 4:

npm install vue-router@next

then import createRouter to create router instance:然后导入createRouter以创建路由器实例:

 import { createRouter,createWebHistory} from 'vue-router'

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: [
     {
      ...
     }
 ]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM