当前位置: 首页 > news >正文

嵌套路由~

### 作业

- App.vue

```vue

<script setup></script>

<template>

<router-link to="/home">首页</router-link>

<router-link to="/profile">个人资料</router-link>

<router-link to="/posts">文章</router-link>

<RouterView></RouterView>

</template>

```

- router.js

```js

const routes = [

    {path: '/',redirect: '/home'},

    {path: '/home',name: 'Home',component: Home},

    {path: '/profile',name: 'Profile',component: Profile},

];

export const router = createRouter({

    history: createWebHistory(),

    routes

});

```

```vue

<script setup></script>

<template>

<h2>User{{ $route.params.id }}</h2>

<!-- 要写出口,子文件 -->

<RouterView></RouterView>

</template>

```

### 笔记

* 在 User 组件的模板中添加 `<router-view></router-view>`,用于渲染匹配的子组件

1. **空路径的嵌套路由**

```js

{

  path: '/user/:id',

  component: User,

  children: [

    { path: '', component: UserHome },

    { path: 'profile', component: UserProfile }

  ]

}

```

2. **命名路由的嵌套**:添加 name 属性:

```js

{

  path: '/user/:id',

  component: User,

  children: [

    { path: '', name: 'user-home', component: UserHome },

    { path: 'profile', name: 'user-profile', component: UserProfile },

    { path: 'posts', name: 'user-posts', component: UserPosts }

  ]

}

```

* 这样可以通过名称进行导航,例如使用 `this.$router.push({ name: 'user-profile', params: { id: userId }})`。


 

http://www.xdnf.cn/news/363277.html

相关文章:

  • leetcode - 双指针问题
  • Linux C语言线程编程入门笔记
  • uni-app 中的条件编译与跨端兼容
  • 区块链详解
  • 独立自主的网络浏览器——Ladybird
  • 类加载器, JVM类加载机制
  • 【PostgreSQL 中插入数据时跳过已存在记录的方法】
  • 阿里云服务器数据库故障排查指南?
  • springboot 加载 tomcat 源码追踪
  • Web端项目系统访问页面很慢,后台数据返回很快,网络也没问题,是什么导致的呢?
  • NVME / DoCA 是什么?
  • 开源数字人框架 AWESOME-DIGITAL-HUMAN 技术解析与应用指南
  • 【Ansible】模块详解
  • 切比雪夫不等式专题习题解析
  • 国联股份卫多多与北京经纬智诚签署战略合作协议
  • 使用Python和TensorFlow实现图像分类的人工智能应用
  • 计算人声录音后电平的大小(dB SPL->dBFS)
  • Leetcode刷题 由浅入深之字符串——541. 反转字符串Ⅱ
  • Spring中除DI之外获取 BEAN 的方式​
  • 数据结构每日一题day18(链表)★★★★★
  • 在自然语言处理任务中,像 BERT 这样的模型会在输入前自动加上一些特殊token
  • MCP(Model Context Protocol)是专为LLM(大语言模型)应用设计的标准化协议
  • CKESC STONE 200A-M 工业级电调技术测评:全场景适配的动力控制核心
  • 【谭浩强】第七章第14题
  • 【C语言】--指针超详解(三)
  • Qwen智能体qwen_agent与Assistant功能初探
  • 昆仑万维一季度营收增长46% AI业务成新增长点
  • epoch、batch size和steps_per_epoch的区别
  • Linux 大于2T磁盘分区
  • FPGA 41 ,ICMP 协议详细解析之构建网络诊断系统( ICMP 协议与 IP 协议理论详细解析 )