自学内容网 自学内容网

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')


原文地址:https://blog.csdn.net/weixin_52645312/article/details/135696726

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!