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

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

相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
发呆的薇薇°3 小时前
vue3 配置@根路径
前端·vue.js
luoluoal3 小时前
基于Spring Boot+Vue的宠物服务管理系统(源码+文档)
vue.js·spring boot·宠物
luckyext3 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)3 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_4 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome