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

【Vue-Router】嵌套路由

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

<template><div><router-view></router-view><hr><h1>我是父路由</h1><div><router-link to="/user">Login</router-link><router-link to="/user/reg" style="margin-left: 10px;">Reg</router-link></div></div>
</template><script setup lang="ts"></script><style scoped></style>

list.json

{"data": [{"name": "面","price":300,"id": 1},{"name": "水","price":400,"id": 2},{"name": "菜","price":500,"id": 3}]}

login.vue

<template><h1>我是列表页面</h1><table cellpadding="0" class="table" border="1"><thead><tr><th>商品</th><th>价格</th><th>操作</th></tr></thead><tbody><tr :key="item.id" v-for="item in data"><th>{{ item.name }}</th><th>{{ item.price }}</th><th><button @click="toDetail(item)">详情</button></th></tr></tbody></table>
</template><script setup lang="ts">
import { data } from './list.json'
import { useRouter } from 'vue-router';const router = useRouter()type Item = {name: string;price: number;id: number;
}const toDetail = (item: Item) => {router.push({// name 对应 router 的 namename: 'Reg',// 不会展示在URL上,存在于内存里params: {id: item.id}})
}
</script><style scoped>
.table {width: 400px;
}
</style>

reg.vue

<template><h1>我是列表页面</h1><button @click="router.back()">返回</button><div style="font-size: 20px;">品牌:{{ item?.name }}</div><div style="font-size: 20px;">价格:{{ item?.price }}</div><div style="font-size: 20px;">id: {{ item?.id }}</div>
</template><script setup lang="ts">
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
import { data } from './list.json';const router = useRouter();
const route = useRoute();
// 返回对象用item接收
console.log(route);const item = data.find(v => v.id === Number(route.params.id))</script><style scoped>
.reg {background-color: green;height: 400px;width: 400px;font-size: 20px;color: white;
}
</style>

index.ts

import { createRouter, createWebHistory, RouteRecordRaw,  } from "vue-router";const routes: Array<RouteRecordRaw> = [{path: "/user",component: () => import("../components/footer.vue"),children:[{path: "",name: 'Login',component: () => import("../components/login.vue")},{path: "reg",name: 'Reg',component: () => import("../components/reg.vue")}]},
]const router = createRouter({history: createWebHistory(),routes
})export default router

App.vue

<template><h1>hello world</h1><hr><router-view></router-view>
</template><script setup lang="ts"></script><style scoped></style>

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/124667.html

相关文章:

  • MySQL索引总结
  • 谷粒商城第十二天-基本属性销售属性管理功能的实现
  • 利用安全区域的概念解决移动端兼容不同手机刘海的问题
  • 数据结构---图
  • 励志长篇小说《周兴和》书连载之十八 内外交困搞发明
  • web基础入门和php语言基础入门 二
  • typeScript 之 Array
  • 【题解】二叉树的前中后遍历
  • 文件操作/IO
  • 基于Java+SpringBoot+vue前后端分离共享汽车管理系统设计实现
  • Mac RN环境搭建
  • log4j教程_编程入门自学教程_菜鸟教程-免费教程分享
  • DP——背包问题
  • 【从零学习python 】29. 「函数参数详解」——了解Python函数参数的不同用法
  • 10个经典战略分析模型,助力洞察市场明确优势
  • C++(Qt)软件调试---将调试工具安装到AeDebug(11)
  • 浅谈限流式保护器在住宅电气防火的应用
  • ChatGPT助力ModStartBlog,博客写作更智能
  • Jpa与Druid线程池及Spring Boot整合(二): spring-boot-starter-data-jpa 踏坑异常处理方案
  • Vue3组件库
  • AUTOSAR从入门到精通-【应用篇】基于 CAN/LIN 总线的智能配电监控系统的研究设计
  • 数据安全服务能力评定资格证书-申请流程
  • 用js快速生成一个简单的css原子库 例如: .mr-18 .pl-18
  • Java鹰眼轨迹服务 轻骑小程序 运动健康与社交案例
  • 【产品经理】微信小程序隐私保护指引
  • springboot创建websocket服务端
  • 网络安全攻防实战:探索互联网发展史
  • pwm接喇叭搞整点报时[keyestudio的8002模块]
  • 配置listener tcps加密 enable SSL encryption for Oracle SQL*Net
  • 【Sklearn】基于逻辑回归算法的数据分类预测(Excel可直接替换数据)