今日讲讲路由配置

下载安装路由

  1. 下载安装路由库
    npm i vue-router

  2. 在 src 中新建 views 文件夹,在其中新建页面

  3. 在 src 中新建一个 router 文件夹,其中新建一个 index.js

    import { createRouter, createWebHashHistory } from 'vue-router';
    // 导入页面
    import index from '../views/index.vue';
    import about from '../views/about.vue';
    // 注册
    const routes = [
    {
    path: '/', // 路径名,首页是/
    name: 'index', // 页面名
    component: index, // 组件,页面对应的文件
    },
    {
    path: '/about',
    name: 'about',
    component: about,
    },
    ];
    // 路由实例
    const router = createRouter({
    history: createWebHashHistory(),
    routes, // 所有的路由
    });
    // 导出
    export default router;

  4. 在 main.js 中安装路由

    import { createApp } from 'vue'
    import App from './App.vue'
    // 导入路由实例
    import router from './router';
    // vue实例
    const app = createApp(App)
    // 在vue实例上安装路由
    app.use(router)
    app.mount('#app')

  5. 在 App.vue 中写出口

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

路由模式

模式有两种: h5 、 hash

复制代码
h5: createWebHistory;http://localhost:5173/about;刷新404

hash:createWebHashHistory;http://localhost:5173/#/about

捕获404页面

  1. 新建一个 404 页面( NotFound )

  2. 导入页面

  3. 在配置数组中添加如下:

    { path: '/:pathMatch(.)', name: 'NotFound', component: NotFound },

重定向

使用redirect属性

复制代码
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
redirect: '/'
},
相关推荐
Senar11 分钟前
Web端选择本地文件的几种方式
前端·javascript·html
烛阴29 分钟前
UV Coordinates & Uniforms -- OpenGL UV坐标和Uniform变量
前端·webgl
姑苏洛言34 分钟前
扫码小程序实现仓库进销存管理中遇到的问题 setStorageSync 存储大小限制错误解决方案
前端·后端
烛阴44 分钟前
JavaScript 的 8 大“阴间陷阱”,你绝对踩过!99% 程序员崩溃瞬间
前端·javascript·面试
lh_12541 小时前
ECharts 地图开发入门
前端·javascript·echarts
jjw_zyfx1 小时前
成熟的前端vue vite websocket,Django后端实现方案包含主动断开websocket连接的实现
前端·vue.js·websocket
Mikey_n2 小时前
前台调用接口的方式及速率对比
前端
周之鸥2 小时前
使用 Electron 打包可执行文件和资源:完整实战教程
前端·javascript·electron
我爱吃朱肉2 小时前
HTMLCSS模板实现水滴动画效果
前端·css·css3
机器视觉知识推荐、就业指导2 小时前
开源QML控件:进度条滑动控件(含源码下载链接)
前端·qt·开源·qml