vite+vue+ts+element-plus从零开发管理后台框架(03)-路由

新建视图

新建src/views/Login.vue,内容如下。

<template>
    这是登录页
</template>

新建src/views/Main.vue,内容如下。

<template>
    这是主页
</template>

新建src/views/errors/404.vue,内容如下。

<template>
    404 page not found
</template>

编辑src/App.vue,内容如下。

<template>
  <router-view />
</template>

路由配置

安装组件

npm install vue-router@4.4.0

新建src/router/index.ts,内容如下。

import { RouteRecordRaw, createRouter, createWebHashHistory } from 'vue-router'


const routes: RouteRecordRaw[] = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue'),
    },
    {
        path: '/',
        name: 'Main',
        component: () => import('@/views/Main.vue'),
    },
    {
        path: '/:catchAll(.*)',
        component: () => import('@/views/errors/404.vue'),
    },
]

const router = createRouter({
    routes,
    history: createWebHashHistory()
})


export default router

编辑src/main.ts,导入并使用路由。

import App from './App.vue'

import router from './router'
app.use(ElementPlus).use(router)

测试

浏览器访问 http://localhost:5173/#/login 网页会显示这是登录页

浏览器访问 http://localhost:5173/#/ 网页会显示这是主页

浏览器访问 http://localhost:5173/#/xxx 或其他任意不存在的路由网页都会显示404 page not found

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值