vue3-setup的实行机遇(早于beforeCreate this为undefined)与传递参数(pro ...

打印 上一主题 下一主题

主题 1310|帖子 1310|积分 3930

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.前言

        setup函数是组合式API的一部分,用于组件创建之前实行逻辑,新的组合式API入口点,用于组件创建前实行逻辑,setup函数接收两个参数props与context。props包含了传递给子组件的所有属性。context包含attrs,slots,emit.
        setup实行的机遇
        在beforeCreate前实行一次,this是undefined.
        setup参数
        props: 值为对象,包含组件外部传递过来,并且组件内部声明接收了的属性。
        context:上下文对象
        attrs:值为对象,组件外部传递但是没有在props设置接收中声明的属性,this.$attrs.
        slots:收到的插槽内容,this.$slots.
        emit:分发自定义事件函数,this.$emit.
2.vue3中的props传参

父组件中利用子组件:参数传递给子组件。
  1. <!-- ParentComponent.vue -->
  2. <template>
  3.   <div>
  4.     <ChildComponent :name="parentName" :age="parentAge" />
  5.   </div>
  6. </template>
  7. <script>
  8. import { defineComponent, ref } from 'vue';
  9. import ChildComponent from './ChildComponent.vue';
  10. export default defineComponent({
  11.   name: 'ParentComponent',
  12.   components: {
  13.     ChildComponent
  14.   },
  15.   setup() {
  16.     const parentName = ref('messi');
  17.     const parentAge = ref(15);
  18.     return {
  19.       parentName,
  20.       parentAge
  21.     };
  22.   }
  23. });
  24. </script>
复制代码
子组件利用setup函数接收参数
  1. <!-- ChildComponent.vue -->
  2. <template>
  3.   <div>
  4.     <p>用户名字: {{ name }}</p>
  5.     <p>用户年龄: {{ age }}</p>
  6.   </div>
  7. </template>
  8. <script>
  9. import { defineComponent } from 'vue';
  10. export default defineComponent({
  11.   name: 'ChildComponent',
  12.   props: {
  13.     name: {
  14.       type: String,
  15.       required: true
  16.     },
  17.     age: {
  18.       type: Number,
  19.       required: true
  20.     }
  21.   },
  22.   setup(props) {
  23.     // 在这里可以使用props.name 和 props.age
  24.     console.log(props.name, props.age);
  25.     return {
  26.       // 返回的数据可以在模板中使用
  27.     };
  28.   }
  29. });
  30. </script>
复制代码
3.context对象emit方法触发自定义事件

父组件
  1. <template>
  2.   <div>
  3.     <ChildComponent :msg="message" @child-click="handleChildClick" />
  4.   </div>
  5. </template>
  6. <script>
  7. import { ref } from 'vue';
  8. import ChildComponent from './ChildComponent.vue';
  9. export default {
  10.   components: {
  11.     ChildComponent
  12.   },
  13.   setup() {
  14.     const message = ref('Hello from Parent');
  15.     const handleChildClick = (childMessage) => {
  16.       console.log(`Received from child: ${childMessage}`);
  17.     };
  18.     return {
  19.       message,
  20.       handleChildClick
  21.     };
  22.   }
  23. };
  24. </script>
复制代码
子组件

  1. <template>
  2.   <div>
  3.     <p>{{ msg }}</p>
  4.     <button @click="sendMessageToParent">Click me</button>
  5.   </div>
  6. </template>
  7. <script>
  8. export default {
  9.   props: {
  10.     msg: String
  11.   },
  12.   setup(props, context) {
  13.     const sendMessageToParent = () => {
  14.       context.emit('child-click', 'Hello from Child');
  15.     };
  16.     return {
  17.       sendMessageToParent
  18.     };
  19.   }
  20. };
  21. </script>
复制代码
   context 对象的 emit 方法被用来触发一个自定义事件 child-click,并将消息从子组件传递给父组件。父组件通过监听 child-click 事件来接收子组件传递的消息,并在控制台中打印出来。
        context 对象的其他属性,如 attrs 和 slots,也可以在 setup 函数中利用,分别用于访问组件的非prop属性和插槽内容。expose 方法可以用来显式地袒露组件实例上的属性或方法,使得父组件可以通过模板引用访问这些属性或方法。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

星球的眼睛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表