【Vue-Router】别名

后台返回来的路径名不合理,但多个项目在使用中了,不方便改时可以使用别名。可以有多个或一个。


First.vue

html 复制代码
<template>
  <h1>First Seciton</h1>
</template>

Second.vueThird.vue代码同理

UserSettings.vue

html 复制代码
<template>
  <h1>UserSettings</h1>
  <router-link to="/settings/children1">children1</router-link>
  <br />
  <router-link to="/settings/children2">children2</router-link>
  <br>
  <button @click="toBackPage">返回</button>
  <hr>
  <router-view></router-view>
  <router-view name="a"></router-view>
  <router-view name="b"></router-view>
</template> 

<script setup lang="ts">
import { useRouter } from 'vue-router';
const router = useRouter();
const toBackPage = () => {
  router.go(-1);
}
</script>

<style scoped></style>

index.ts

ts 复制代码
import { createRouter, createWebHistory } from 'vue-router'
import First from '../components/First.vue'
import Second from '../components/Second.vue'
import Third from '../components/Third.vue'
import UserSettings from '../components/UserSettings.vue'

export const router = createRouter({
  history: createWebHistory(),
  // alias 起别名,都可以访问同一页面
  alias: ['/user-settings', '/Settings'],
  routes: [
    {
      path: '/settings',
      component: UserSettings,
      children: [
        {
          path: 'children1',
          components: {
            default: First,
            a: Second,
            b: Third,
          },
        },
        {
          path: 'children2',
          components: {
            default: Third,
            a: Second,
            b: First,
          },
        },
      ]
    },

  ],
})

App.vue

html 复制代码
<template>
  <h1>Nested Named Views</h1>
  <hr>
  <router-view></router-view>
  <hr>
</template>

<script setup lang="ts">

</script>

<style scoped></style>


相关推荐
艾小码4 分钟前
手把手教你实现一个EventEmitter,彻底告别复杂事件管理!
前端·javascript·node.js
幸福摩天轮14 分钟前
npm发布包
前端
前端AK君16 分钟前
Gitlab 线上合并冲突的坑
前端
ze_juejin16 分钟前
ES6 Module 深入学习
前端
章丸丸19 分钟前
Tube - Studio Videos
前端·后端
因吹斯汀1 小时前
一饭封神:当AI厨神遇上你的冰箱,八大菜系大师在线battle!
前端·vue.js·ai编程
再学一点就睡1 小时前
NATAPP 内网穿透指南:让本地项目轻松 “走出去”
前端
拜无忧1 小时前
2025最新React项目架构指南:从零到一,为前端小白打造
前端·react.js·typescript
稻草人不怕疼1 小时前
记一次从“按钮点不动”到“窗口派发缺失”的排查过程
前端
irving同学462382 小时前
TypeORM 列装饰器完整总结
前端·后端·nestjs