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 中加入代码

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

相关推荐
matlab的学徒16 小时前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
从零开始学习人工智能16 小时前
快速搭建B/S架构HTML演示页:从工具选择到实战落地
前端·架构·html
虫虫rankourin17 小时前
在 create-react-app (CRA) 创建的应用中使用 react-router-dom v7以及懒加载的使用方法
前端·react.js
小刘鸭地下城17 小时前
Web安全必备:关键 HTTP 标头解析
前端
yddddddy17 小时前
html基本知识
前端·html
荣达18 小时前
koa洋葱模型理解
前端·后端·node.js
xiaoyan201518 小时前
Electron38-Winchat聊天系统|vite7+electron38+vue3电脑端聊天Exe
vue.js·electron·vite
reembarkation18 小时前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation18 小时前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
KenXu18 小时前
F2C-PTD工具将需求快速转换为代码实践
前端