How to write router3 and router4 in the vue project! ! Attention please! ! ! !

    Pay attention to what version your vue-router is! ! ! It is really easy for a novice Xiaobai (like me!!!) to make mistakes when creating a new index.js file by himself! ! ! !

I downloaded the router4 version before, but I have been learning to use the router3 writing method when I first started learning, so I am used to using the router3 writing method when creating a new project! ! ! You really need to pay attention to this, otherwise it won't work...

The writing method of router3 uses vue.use(router)

import Vue from 'vue'  //为了使用路由插件
import VueRouter from 'vue-router'  //导入路由
import Home from '../components/HelloWorld' //初始化加载主页面


Vue.use(VueRouter)
var routes = [{
    path: '/',
    name: 'HelloWorld',
    component: Home
},
// {
//     path: '/my',
//     name: 'My',
//     component: () => import('../components/My.vue') 
// }
]

const router = new VueRouter({
routes  //es6的属性增强写法
})

export default router

How to write router4

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

 ! ! ! Learning Record 1! ! ! !

Guess you like

Origin blog.csdn.net/qq_66061193/article/details/129490551