报错:Uncaught TypeError: Cannot read properties of undefined (reading ‘use’)

原因:Vue-router版本问题,在vue-router3可在router/index.js中安装router插件,但4不支持

解决:
1、在router/index.js中,导出{createRouter, createWebHistory}两个函数
使用createRouter来创建路由对象
使用createWebHistory函数赋值给history属性,因为是vue-router4所以必须有history属性,否则会报错
Uncaught Error: Provide the “history” option when calling “createRouter()”: https://next.router.vuejs.org/api/#history.

2、然后在main.js文件中安装路由插件

router/index.js

import {createRouter, createWebHistory} from 'vue-router'
const routes = [
  {
    path: '/hello',
    component: () => import('../components/HelloWorld')
  }
]
export default createRouter({
  history: createWebHistory(),
  routes
})

main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)
app.use(router)
app.mount('#app')
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐