【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>


相关推荐
newxtc18 分钟前
【爱给网-注册安全分析报告-无验证方式导致安全隐患】
前端·chrome·windows·安全·媒体
一个很帅的帅哥35 分钟前
axios(基于Promise的HTTP客户端) 与 `async` 和 `await` 结合使用
javascript·网络·网络协议·http·async·promise·await
刘志辉1 小时前
vue传参方法
android·vue.js·flutter
dream_ready1 小时前
linux安装nginx+前端部署vue项目(实际测试react项目也可以)
前端·javascript·vue.js·nginx·react·html5
编写美好前程1 小时前
ruoyi-vue若依前端是如何防止接口重复请求
前端·javascript·vue.js
flytam1 小时前
ES5 在 Web 上的现状
前端·javascript
喵喵酱仔__1 小时前
阻止冒泡事件
前端·javascript·vue.js
GISer_Jing1 小时前
前端面试CSS常见题目
前端·css·面试
某公司摸鱼前端1 小时前
如何关闭前端Chrome的debugger反调试
javascript·chrome
八了个戒2 小时前
【TypeScript入坑】什么是TypeScript?
开发语言·前端·javascript·面试·typescript