描述:
// message.vue
<template>
<li v-for="m in messageList" :key="m.id">
<router-link
:to="{ name: 'Detail', query: { id: m.id, title: m.title } }"
>{{ m.title }}</router-link
>
<button @click="pushShow(m)">push 查看</button>
</li>
<hr />
<router-view></router-view>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
let messageList = [
{ id: "004", title: "message004" },
{ id: "005", title: "message005" },
{ id: "006", title: "message006" },
];
const router = useRouter();
const pushShow = (m: { id: string; title: string }) => {
router.push({ name: "Detail", query: { id: m.id, title: m.title } });
};
</script>
<style scoped lang="less"></style>
// detail.vue
<template>
<li v-for="m in messageList" :key="m.id">
{{ m.title }}
</li>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { useRoute } from "vue-router";
let messageList = reactive([
{ id: "001", title: "message001" },
{ id: "002", title: "message002" },
{ id: "003", title: "message003" },
]);
const route = useRoute();
</script>
<style scoped lang="less"></style>
1
sjhhjx0122 2022-12-05 13:47:46 +08:00
最简单的就是,messageList 并不在 detail 里维护,detail 只渲染单条,在 message.vue 维护 messageList ,然后循环 detail props 传入单条数据
|
2
leoQaQ 2022-12-05 14:16:39 +08:00
用 watch ?
|
3
hevi 2022-12-05 14:38:13 +08:00
onMounted 的时候,把传进来的值 push ,然后消除 query
或者 watch ,push 后消除 query |
4
yigecaiji 2022-12-05 15:15:07 +08:00 via Android
不用 push ,用 set ?
|