【Vue-Router】别名

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

在这里插入图片描述
First.vue

<template>
  <h1>First Seciton</h1>
</template>

Second.vueThird.vue代码同理

UserSettings.vue

<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

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: {
            // 默认对应 router-view
            default: First,
            // router-view 对应的 name="a" 时访问
            a: Second,
            b: Third,
          },
        },
        {
          path: 'children2',
          components: {
            default: Third,
            a: Second,
            b: First,
          },
        },
      ]
    },

  ],
})

App.vue

<template>
  <h1>Nested Named Views</h1>
  <hr>
  <router-view></router-view>
  <hr>
</template>

<script setup lang="ts">

</script>

<style scoped></style>

当跳转children1时,默认为 first,a为second,b为third。

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小秀_heo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值