Vue3 Setup语法糖,父子组件之间传各种数据类型方法

Vue3 Setup语法糖,父子组件之间传各种数据类型方法

一、父组件向子组件使用语法糖传字符串类型

        1.父组件

msg是你的自定义的变量名,用于传给子组件,content是你父组件的要传给子组件的数据。


   
   
    
    
  1. <template>
  2. <div>
  3. <Hello :msg="content" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import { ref } from "vue";
  8. import Hello from "../components/HelloWorld.vue";
  9. const content = ref( "那就这样吧");
  10. </script>

        2.子组件

语法糖中可以直接在props = defineProps()的方法里面写你穿过来的数据,最后在模板中调用就ok了


   
   
    
    
  1. <template >
  2. <div >
  3. <h 1 >{ { props.msg }} < /h 1 >
  4. < /div >
  5. < /template >
  6. <script setup >
  7. const props = defineProps({
  8. msg: String,
  9. });
  10. < /script >

 最终呈现处这样的效果

二、父组件向子组件使用语法糖传数组类型

1.父组件传递

 组件中DateList是你声明传递给子组件的名称,ArrayList是你要传递给子组件的数据。


   
   
    
    
  1. <template >
  2. <div >
  3. <Hello :DateList = "ArrayList" / >
  4. < /div >
  5. < /template >
  6. <script setup >
  7. import { reactive } from "vue";
  8. import Hello from "../components/HelloWorld.vue";
  9. const ArrayList = reactive([
  10. {
  11. id: 1,
  12. name: "法外",
  13. },
  14. {
  15. id: 2,
  16. name: "狂徒",
  17. },
  18. {
  19. id: 3,
  20. name: "张三",
  21. },
  22. ]);
  23. < /script >

2.子组件接收

语法糖中可以直接在props = defineProps()的方法里面写你穿过来的数据,最后在模板中循环输出就ok了


   
   
    
    
  1. <template >
  2. <div >
  3. <h 1 >{ { props.msg }} < /h 1 >
  4. <div v-for = "(item, index) in props.DateList" : key = "item.id" >
  5. { { item.name }}
  6. < /div >
  7. < /div >
  8. < /template >
  9. <script setup >
  10. const props = defineProps({
  11. msg: String,
  12. DateList: Array,
  13. });
  14. console.log(props.DateList);
  15. < /script >

呈现效果

三.子组件向父组件使用语法糖传字值

1.子组件

 子传父需要用到事件来传递, let emigFun = defineEmits(["handLer"]); 使用defineEmits语法糖,创建一个自定义事件,然后根据自定义事件来传递你想要传递的类型(如下图)


   
   
    
    
  1. <template >
  2. <div >
  3. <div @click = "fun" >
  4. 子传父
  5. < /div >
  6. < /div >
  7. < /template >
  8. <script setup >
  9. import { reactive } from "vue";
  10. let emigFun = defineEmits([ "handLer"]); / /使用defineEmits语法糖的方法来创建自定义事件。
  11. const dat = reactive([
  12. {
  13. name: 123,
  14. id: 1,
  15. },
  16. {
  17. name: 123,
  18. id: 1,
  19. },
  20. {
  21. name: 123,
  22. id: 1,
  23. },
  24. {
  25. name: 123,
  26. id: 1,
  27. },
  28. ]);
  29. const fun = () = > {
  30. emigFun( "handLer", dat); / /左边是你的自定义事件名,右边是你要传的数据。
  31. };
  32. < /script >

2.父组件

父组件这边就可以直接使用你子组件创建的自定义事件,加上你父组件的事件名,通过形参的方式成功拿到子组件的数据(如下图)


   
   
    
    
  1. <template >
  2. <div >
  3. <Hello @handLer = "hand" / >
  4. <div >
  5. 点击拿到子组件的值
  6. < /div >
  7. < /div >
  8. < /template >
  9. <script setup >
  10. import Hello from "../components/HelloWorld.vue";
  11. const hand = (v) = > {
  12. console.log(v);
  13. };
  14. < /script >

效果展示

总结

另外还有一些使用setup语法糖的一些问题,下一篇给大家讲一下,其次Vue父子组件互相传值,传来传去新手容易懵,多理清思路,慢慢来,还有什么更好的见解,或者想法请在下方评论探讨。

Vue3 Setup语法糖,父子组件之间传各种数据类型方法

一、父组件向子组件使用语法糖传字符串类型

        1.父组件

msg是你的自定义的变量名,用于传给子组件,content是你父组件的要传给子组件的数据。


   
   
  
  
  1. <template>
  2. <div>
  3. <Hello :msg="content" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import { ref } from "vue";
  8. import Hello from "../components/HelloWorld.vue";
  9. const content = ref( "那就这样吧");
  10. </script>

        2.子组件

语法糖中可以直接在props = defineProps()的方法里面写你穿过来的数据,最后在模板中调用就ok了


   
   
  
  
  1. <template >
  2. <div >
  3. <h 1 >{ { props.msg }} < /h 1 >
  4. < /div >
  5. < /template >
  6. <script setup >
  7. const props = defineProps({
  8. msg: String,
  9. });
  10. < /script >

 最终呈现处这样的效果

二、父组件向子组件使用语法糖传数组类型

1.父组件传递

 组件中DateList是你声明传递给子组件的名称,ArrayList是你要传递给子组件的数据。


   
   
  
  
  1. <template >
  2. <div >
  3. <Hello :DateList = "ArrayList" / >
  4. < /div >
  5. < /template >
  6. <script setup >
  7. import { reactive } from "vue";
  8. import Hello from "../components/HelloWorld.vue";
  9. const ArrayList = reactive([
  10. {
  11. id: 1,
  12. name: "法外",
  13. },
  14. {
  15. id: 2,
  16. name: "狂徒",
  17. },
  18. {
  19. id: 3,
  20. name: "张三",
  21. },
  22. ]);
  23. < /script >

2.子组件接收

语法糖中可以直接在props = defineProps()的方法里面写你穿过来的数据,最后在模板中循环输出就ok了


   
   
  
  
  1. <template >
  2. <div >
  3. <h 1 >{ { props.msg }} < /h 1 >
  4. <div v-for = "(item, index) in props.DateList" : key = "item.id" >
  5. { { item.name }}
  6. < /div >
  7. < /div >
  8. < /template >
  9. <script setup >
  10. const props = defineProps({
  11. msg: String,
  12. DateList: Array,
  13. });
  14. console.log(props.DateList);
  15. < /script >

呈现效果

三.子组件向父组件使用语法糖传字值

1.子组件

 子传父需要用到事件来传递, let emigFun = defineEmits(["handLer"]); 使用defineEmits语法糖,创建一个自定义事件,然后根据自定义事件来传递你想要传递的类型(如下图)


   
   
  
  
  1. <template >
  2. <div >
  3. <div @click = "fun" >
  4. 子传父
  5. < /div >
  6. < /div >
  7. < /template >
  8. <script setup >
  9. import { reactive } from "vue";
  10. let emigFun = defineEmits([ "handLer"]); / /使用defineEmits语法糖的方法来创建自定义事件。
  11. const dat = reactive([
  12. {
  13. name: 123,
  14. id: 1,
  15. },
  16. {
  17. name: 123,
  18. id: 1,
  19. },
  20. {
  21. name: 123,
  22. id: 1,
  23. },
  24. {
  25. name: 123,
  26. id: 1,
  27. },
  28. ]);
  29. const fun = () = > {
  30. emigFun( "handLer", dat); / /左边是你的自定义事件名,右边是你要传的数据。
  31. };
  32. < /script >

2.父组件

父组件这边就可以直接使用你子组件创建的自定义事件,加上你父组件的事件名,通过形参的方式成功拿到子组件的数据(如下图)


   
   
  
  
  1. <template >
  2. <div >
  3. <Hello @handLer = "hand" / >
  4. <div >
  5. 点击拿到子组件的值
  6. < /div >
  7. < /div >
  8. < /template >
  9. <script setup >
  10. import Hello from "../components/HelloWorld.vue";
  11. const hand = (v) = > {
  12. console.log(v);
  13. };
  14. < /script >

效果展示

总结

另外还有一些使用setup语法糖的一些问题,下一篇给大家讲一下,其次Vue父子组件互相传值,传来传去新手容易懵,多理清思路,慢慢来,还有什么更好的见解,或者想法请在下方评论探讨。

猜你喜欢

转载自blog.csdn.net/weixin_64310738/article/details/129034444