Vue3-路由安装、配置与使用

一、安装router

npm i vue-router

二、创建router文件夹

创建index.ts文件

import {createRouter,createWebHashHistory} from 'vue-router'
const routes =[
	{
		path:'/',
		redirect:'/login'
	},
	{
	    name:'login',
	    path:'/login',
	    component:()=>import('@/views/login/index.vue')
        meta: {
          title: '登录', 
        },
        children: [
        {
            path: '/home',
            component: () => import('@/views/home/index.vue'),
            meta: {
              title: '首页',
            },
        }]
	},
    {
        path: '/404',
        component: () => import('@/views/404/index.vue'),
        name: '404',
        meta: {
          title: '404',
        }  
    },
]
const router = createRouter({
	history:createWebHashHistory(),
	routes,
    //滚动行为
    scrollBehavior() {
        return {
            left: 0,
            top: 0,
        }
    },
})
export default router

三、挂载路由

//main.ts
import { createApp } from 'vue'
import App from '@/App.vue'
//引入路由
import router from './router'
//获取应用实例对象
const app = createApp(App)
//注册模板路由
app.use(router)
//将应用挂载到挂载点上
app.mount('#app')

你可能感兴趣的:(vue.js,javascript,前端)