vue-Router 路由(常量路由)

1、安装

复制代码
pnpm i vue-router

2、新建文件:src/routes.ts

TypeScript 复制代码
import { RouteRecordRaw } from 'vue-router'

export const constantRoute: RouteRecordRaw[] = [
  {
    //
    path: '/',
    redirect: '/login',
  },
  {
    //
    path: '/login',
    component: () => import('@/views/Login/index.vue'),
    name: 'Login',
    meta: {
      title: '登录',
    },
  },
  {
    //登录成功展示数据的页面 main.vue
    path: '/layout',
    component: () => import('@/layout/index.vue'),
    name: 'layout',
    meta: {
      title: 'layout',
    },
  },
  {
    path: '/404',
    component: () => import('@/views/404/index.vue'),
    name: '404',
    meta: {
      title: '404',
    },
  },
  {
    // 任意路由(无效或者不存在的路径 跳转至404)
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/404/index.vue'),
    name: 'Any',
    meta: {
      title: '任意',
    },
  },
]

3、新建文件:src/index.ts

TypeScript 复制代码
import { createRouter, createWebHashHistory } from 'vue-router'
import { constantRoute } from './routes'
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoute,
  //   滚动行为
  scrollBehavior() {
    // ...
    return {
      left: 0,
      top: 0,
    }
  },
})

export const setupRouter = (app: any) => {
    app.use(router)
  }
export default router

4、main.ts 引入

TypeScript 复制代码
// 路由
import { setupRouter } from './router'
// 创建实例
const setupAll = async () => {
  const app = createApp(App)
  await setupRouter(app)
  app.mount('#app')
}

setupAll()

5、app.vue 中加入代码

以上完成 ,输入不同路径就可以跳转到对应页面了。

相关推荐
竹林8181 分钟前
用Viem替代ethers.js:从一次签名失败到完整迁移的实战记录
前端·javascript
之歆6 分钟前
DAY08_CSS浮动与行内块布局实战指南(上)
前端·css
light blue bird27 分钟前
主子端台二分法任务汇总组件
前端·数据库·.net·桌面端winform
jeffwang1 小时前
我做了个让 AI 看屏幕跑测试的工具,因为 Playwright 测不了我的 Flutter Web
前端
HSunR2 小时前
dify 搭建ai作业批改流
开发语言·前端·javascript
代码不加糖2 小时前
2026 跨境电商独立站实战:从 0 到 1 搭建高转化 SaaS 商城(附源码)
开发语言·前端·javascript
亲亲小宝宝鸭2 小时前
拖一拖控件,拖出个问卷(低代码平台)
前端·低代码
江南十四行2 小时前
ReAct Agent 基本理论与项目实战(一)
前端·react.js·前端框架
We་ct3 小时前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·leetcode·typescript·动态规划
小呆呆6663 小时前
Codex 穷鬼大救星
前端·人工智能·后端