21

Created a simple Vue project using the CLI:

vue create my-project

Wanted to add two pages, so installed the latest version of vue-router (which is currently v3.4.8) and followed the vue mastery tutorial for routing.

This is what my router.js file looks like:

import { createWebHistory, createRouter } from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'Home', component: Home },
    { path: '/about', name: 'About', component: About },
  ]
})

export default router

And of course, this is what my main.js file looks like:

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

createApp({
  template: `
  <div>
    <router-link to='/'>Home</router-link>
    <router-link to='/create'>Create</router-link>
  </div>
  `
})
.use(router)
.mount('#app')

Both of the Home and About components don't really have much in them, this is what they look like:

<template>
  <div>TODO: Home</div>
</template>

<script>
  export default {
    name: 'Home'
  }
</script>

Anyway, all of this to say that I am getting the following error on:

Uncaught TypeError: Object(...) is not a function

at eval (router.js?41cb:5)

This is specifically on createRouter

Have I done something wrong?

Edit: as Boussadjra Brahim pointed out, originally createWebHistory was just being passed in without being a function call. I've since updated the code to include this.

Interestingly enough, once that was done, the error is not happening upon it's call.

4

3 回答 3

55

当您使用 Vue 3 安装 Vue router 3 时会出现此问题,因此您应该卸载当前版本:

npm uninstall vue-router --save

并通过以下方式安装新的:

npm i vue-router@next --save

例子

于 2020-10-28T15:33:24.897 回答
2

对于 vue3,您应该使用以下命令安装 @4 包:

npm install vue-router@4

如果您遇到问题,请参阅以下迁移指南:

https://next.router.vuejs.org/guide/migration/

于 2021-02-21T10:29:27.230 回答
0

就我而言,情况正好相反(我遇到了同样的错误),我安装了 vue 2.6 和 Vue Router 4,所以我不得不将 Vue Router 降级到 3.5

于 2022-02-23T09:50:39.227 回答