问题
Learning vuejs3 I created new @vue/cli app with command
vue create myapp
with vuejs 3 selected I added Router to my project and added Router reference in my src/main.js :
import { createApp } from 'vue'
import { createRouter/*, createWebHistory */ } from 'vue-router'
import WelcomeScreen from './pages/WelcomeScreen.vue'
import UsersList from './pages/UsersList.vue'
import App from './App.vue'
const router = createRouter({
// history: createWebHistory(),
mode: 'history',
routes: [
{ path: '/', component: WelcomeScreen },
{ path: '/users', component: UsersList }
]
})
const app = createApp(App)
app.use(router)
app.mount('#app')
But In the console I see warning :
"export 'createRouter' was not found in 'vue-router'
and error :
main.js?56d7:10 Uncaught TypeError: Object(...) is not a function
In my package.json I have :
"dependencies": {
"core-js": "^3.6.5",
"mitt": "^2.1.0",
"vue": "^3.0.0",
"vue-router": "^3.4.8"
},
and
$ vue --version
@vue/cli 4.5.8
Which way is valid? Also are some some vuejs3 tutorials for @vue/cli? I found some vuejs 3 tutorials, but not for @vue/cli and that raise some confusion...
Say in my @vue/cli with vue2 I use file src/router/index.js, but I do not remember if I have created manually...
Thanks!
回答1:
You need install vue-router like this for vue3:
npm install vue-router@next --save
Router.js
import { createApp } from 'vue'
import App from './App.vue'
import { createRouter, createWebHistory } from "vue-router"
const routeInfos = [
{
path : "/",
component : HomePage
},
{
path : "/favorites",
component : FavoritesPage
}
]
const router = createRouter({
history : createWebHistory(),
routes : routeInfos
})
export default router;
import In Main.js
import router from "./router"
createApp(App).use(router).mount('#app')
来源:https://stackoverflow.com/questions/64623889/how-to-add-router-to-vue-cli-app-with-vuejs-3