vue3子父组件之间的调用

子组件:

capacityIndex.vue

父组件:

   index.vue

A.子组件获取父组件属性

1.在父组件中引用子组件

import capacityIndex from "./capacityIndex";
<capacityIndex :tankInfo="tankInfo" :deviceNameInfo="deviceNameInfo" ref="sonMethodRef"></capacityIndex>

其中tankInfo和device Name Info为子组件需要的属性

2.子组件接收父组件传递的属性

const props = defineProps({
  // 油罐编号
  tankInfo: {
    type: String,
    default: ""
  },
  // 液位仪编号
  deviceNameInfo: {
    type: String,
    default: ""
  },
})

3.子组件调用属性

/** 查询罐容信息列表 */
function getList() {
  loading.value = true;
  queryParams.value.tankId = props.tankInfo;
  queryParams.value.deviceName = props.deviceNameInfo;
  listTankCapacity(queryParams.value).then(response => {
    tankCapacityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

其中props.tankInfo就是调用了传递过来的参数。

B.父组件调用子组件方法

1.父组件调用子组件时定义ref为sonMethodRef

<capacityIndex :tankInfo="tankInfo" :deviceNameInfo="deviceNameInfo" ref="sonMethodRef"></capacityIndex>

2.在父组件中定义ref

const sonMethodRef = ref(null)

3.父组件调用子组件暴漏的方法

function capaCityInfo(row){
  capaCityTitle.value = "罐容信息";
  capaCityOpen.value = true;
  tankInfo.value = row.id;
  deviceNameInfo.value = row.deviceName;
  nextTick(() => {
    sonMethodRef.value.getList();
  })
}

其中

nextTick(() => {
  sonMethodRef.value.getList();
}) 就是调用的过程。

4.子组件暴漏父组件需要的方法,getList互相对应。

defineExpose({
  getList
})
function getList() {
  loading.value = true;
  queryParams.value.tankId = props.tankInfo;
  queryParams.value.deviceName = props.deviceNameInfo;
  listTankCapacity(queryParams.value).then(response => {
    tankCapacityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3和TypeScript中,组件可以通过使用`props`和`emit`来调用组件的方法。下面是一个简单的示例,展示了如何在组件调用组件的方法: 组件(ParentComponent.vue): ```vue <template> <div> <ChildComponent :message="parentMessage" @childEvent="handleChildEvent" /> </div> </template> <script lang="ts"> import { defineComponent, PropOptions } from 'vue' import ChildComponent from './ChildComponent.vue' export default defineComponent({ name: 'ParentComponent', components: { ChildComponent }, setup() { const parentMessage = 'Hello from parent' const handleChildEvent = (eventData: any) => { console.log('Received child event:', eventData) // 在这里可以对子组件的事件进行处理 } return { parentMessage, handleChildEvent } as PropOptions<ChildComponent> & {} } }) </script> ``` 子组件(ChildComponent.vue): ```vue <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ name: 'ChildComponent', props: { message: String, }, methods: { sendMessage() { this.$emit('childEvent', this.message) // 触发名为 'childEvent' 的事件,并传递当前组件的 'message' prop 作为参数 } } }) </script> ``` 在上面的示例中,组件通过`<ChildComponent :message="parentMessage" @childEvent="handleChildEvent" />`来将`parentMessage`传递给子组件,并通过`@childEvent`监听子组件的`childEvent`事件。当子组件触发该事件时,组件将通过`handleChildEvent`方法处理该事件。在子组件中,我们有一个按钮,当点击该按钮时,会触发名为`childEvent`的事件,并将当前的`message` prop作为参数传递给组件。在组件中,我们通过监听这个事件来处理子组件传递过来的数据。 请注意,这只是一个简单的示例,实际情况可能更复杂。你可以根据具体需求调整代码,以满足你的项目要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wengelovelian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值