父组件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34<script setup> import { ref } from "vue"; import getPosts from "../composibles/getPosts"; import PostList from "../components/PostList.vue"; import Spinner from "../components/Spinner.vue"; import TagCloud from "../components/TagCloud.vue"; const {posts,load}=getPosts(); load(); </script> <template> <div class="home"> <div v-if="posts" class="layout"> <PostList :posts="posts" /> <TagCloud :posts="posts" v-if="posts!=null"/> </div> <div v-else> <Spinner/> </div> </div> </template> <style scoped> .home{ max-width: 1200px; margin:0 auto; padding:10px; } .layout{ display: grid; grid-template-columns: 3fr 1fr; gap:20px; } </style>
子组件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50<script setup> import { ref,watch} from "vue"; const tags=ref([]); const tagSet=new Set(); const props = defineProps({ posts:Array, }); watch( () => props.posts, (val, prevVal) => { val.forEach ((item)=>{ item.tags.forEach((tag)=> tagSet.add(tag) ) }); tags.value=[...tagSet]; } ) </script> <template> <div class="tag-cloud"> <h3>标签</h3> <div v-for="tag in tags" :key="tag"> <router-link :to="{name:'Tag',params:{tag:tag}}">#{{tag}}</router-link> </div> </div> </template> <style scoped> .tag-cloud{ padding:10px; } .tag-cloud h3{ border-bottom:1px solid #eee; padding:16px 8px; color:#444; } .tag-cloud div{ display: inline-block; padding:10px; } .tag-cloud a{ color:#ccc; text-decoration: none; } </style>
最后
以上就是现代铃铛最近收集整理的关于vue3父组件向子组件传值,异步数据,子组件需要监听才能取到的全部内容,更多相关vue3父组件向子组件传值内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复