npm i vue-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')