美文网首页
vue3父子组件传值(响应式)

vue3父子组件传值(响应式)

作者: kalrase | 来源:发表于2023-07-10 11:43 被阅读0次

    父组件

     <card />
        <card :user="form.user" />
        <van-button @click="handleChoice">随机</van-button>
    
    
    const form = reactive({
      user: {
        name: '阿三',
        job: 'famer',
        info: {
          address: '东大街',
          like: '钓鱼'
        }
      }
    })
    const handleChoice = () => {
      form.user = {
        name: '张飞',
        job: 'fighting',
        info: {
          address: '桃园',
          like: 'drink'
        }
      }
    }
    

    子组件

    <template>
      <div>
        <h3>
          <p>{{form.user.name}}</p>
          <p>{{form.user.job}}</p>
          <p>{{form.user.info.address}}</p>
          <p>{{form.user.info.like}}</p>
        </h3>
      </div>
    </template>
    
    <script setup>
    import { defineProps } from 'vue'
    const form = defineProps({
      user: {
        type: Object,
        default: () => {
          return {
            name: 'XX',
            job: 'XX',
            info: {
              address: 'XX',
              like: 'XX'
            }
          }
        }
      }
    })
    </script>
    
    <style lang="scss" scoped></style>
    
    image.png
    image.png

    相关文章

      网友评论

          本文标题:vue3父子组件传值(响应式)

          本文链接:https://www.haomeiwen.com/subject/hwueudtx.html