Vue3调用子组件方法,清空数据ref,2022/6/4pm

父组件

<template>
  <div class="microstores">
    <div class="container">
   
    <!-- 搜索模块 -->
    <div class="search">
    <el-row :gutter="20" class="row-bg" justify="space-between">
    <el-col :span="8">
      <span class="labletitle">Start Time :</span>
       <!-- 子组件 -->
      <dataPicker  ref="startValueRef"></dataPicker>
    </el-col>
    
       </div>
    </el-col>
  </el-row>
  </div>

    </div>

  </div>
</template>

<script lang="ts" >
import { defineComponent, onMounted, toRefs, reactive, ref } from 'vue'
import dataPicker from '@/components/dataPicker.vue'
export default defineComponent({
  name: 'Microstores',
  components: {

    dataPicker
  },
  setup () {

    const startValue = ref() 
    const startValueRef = ref()
    const data = reactive({
      handleReset () {
        startValueRef.value.startValues = ''
      
      }
    })
    return {
      ...toRefs(data), startValue,  startValueRef// 日期组件

    }
  }

})
</script>
<style lang="less" scoped>

</style>
子组件
<template>
  <div class="about">
    <!-- {{startValues}} -->
   <el-date-picker
        style="width:100%;height:40px"
        v-model="startValues"
        type="datetime"
        format="YYYY/MM/DD hh:mm:ss"
      >
       <template #default="cell">
        <div class="cell" :class="{ current: cell.isCurrent }">
          <span class="text">{{ cell.text }}</span>
        </div>
      </template>
      </el-date-picker>
  </div>
</template>

<script lang="ts">
import { defineComponent, toRefs, reactive, computed } from 'vue'
export default defineComponent({
  name: 'dataPicker',
  setup (props, ctx) {
    const data = reactive({
      startValues: '',
      

    })
    return {
      ...toRefs(data)

    }
  }

})
</script>
<style lang="less" scoped>

</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,要调用组件方法,可以通过将ref绑定在组件上的方式来实现。首先,在父组件中声明一个ref变量,用于获取组件的引用。然后,在组件中使用`defineExpose`方法将需要暴露的方法或属性公开出来。最后,可以通过ref变量来调用组件方法。 举个例,假设有一个名为ChildComponent的组件,其中定义了一个名为`childMethod`的方法。在父组件中,我们可以通过ref来获取组件的引用,并调用组件方法。 ```html <template> <ChildComponent ref="childRef"></ChildComponent> <button @click="callChildMethod">调用组件方法</button> </template> <script setup> import { ref } from "vue"; import ChildComponent from "@/components/ChildComponent.vue"; const childRef = ref(null); const callChildMethod = () => { childRef.value.childMethod(); }; return { childRef, callChildMethod, }; </script> ``` 在上述代码中,我们使用`ref`声明了一个名为`childRef`的变量,并将其绑定在ChildComponent组件上。然后,在`callChildMethod`方法中,我们通过`childRef.value`来访问组件的实例,并调用其中的`childMethod`方法。 请注意,为了在Vue3中正确使用ref获取组件方法组件需要使用`defineExpose`方法将需要暴露的方法或属性公开出来。这样父组件才能通过ref访问到组件方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue3 的组件通信以及ref的使用&v-model](https://blog.csdn.net/gao_xu_520/article/details/125596347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值