Home > Web Front-end > uni-app > What should I do if uniapp fails to call a subcomponent method?

What should I do if uniapp fails to call a subcomponent method?

PHPz
Release: 2023-04-27 09:31:07
Original
770 people have browsed it

Recently, when I was using uniapp to develop a small program, I encountered a problem: calling the subcomponent method failed. After some research and debugging, I found a solution and now share it with you.

First, let’s take a look at the scenario where this problem occurs. A custom component is introduced into a page, and there is a method in the component that needs to be called externally. We can achieve this by defining the method in the methods of the component object and calling the method inside the component. However, when actually calling, we found that this method always returns a null value, as shown below:

// 引入组件
<template>
  <custom-component ref="customComponent"></custom-component>
</template>

<script>
  import customComponent from '@/components/custom-component.vue'
  export default {
    components: {
      customComponent
    },
    mounted() {
      // 调用子组件方法
      const data = this.$refs.customComponent.customMethod()
      console.log(data) // 输出:undefined
    }
  }
</script>

// custom-component.vue
<template>
  <div>这是一个自定义组件</div>
</template>

<script>
  export default {
    methods: {
      customMethod() {
        return '这是从子组件返回的数据'
      }
    }
  }
</script>
Copy after login

Here we try to call the customMethod method of the child component in the parent component and print the return value of the method . However, in the console we find that the return value is undefined.

After investigation, it was found that this is because the communication method of components in uniapp is different from the communication method of Vue's native components. In uniapp, we need to use uni.$emit to send events and listen to the event in the component. The following is the correct example:

// 引入组件
<template>
  <custom-component @customEvent="onCustomEvent"></custom-component>
</template>

<script>
  import customComponent from '@/components/custom-component.vue'
  export default {
    components: {
      customComponent
    },
    methods: {
      onCustomEvent(data) {
        console.log(data) // 输出:这是从子组件返回的数据
      }
    }
  }
</script>

// custom-component.vue
<template>
  <div>这是一个自定义组件</div>
</template>

<script>
  export default {
    methods: {
      customMethod() {
        // 向父组件发送事件
        this.$emit('customEvent', '这是从子组件返回的数据')
      }
    }
  }
</script>
Copy after login

In this example, we use @customEvent to listen to the event of the subcomponent, and process the data passed from the subcomponent in the onCustomEvent method. It should be noted that the method of sending events needs to be done in the child component, rather than calling the method of the child component in the parent component.

This is because in uniapp, the parent component cannot directly call the child component's method. Instead, we need to use events for data transfer and component communication.

To sum up, the component communication methods of uniapp and native Vue are slightly different. In uniapp, we need to communicate between components through events. Especially when calling the method of the subcomponent, we need to use $emit to send the event and listen to the event in the subcomponent to achieve this.

I hope this article is helpful to everyone, thank you for reading!

The above is the detailed content of What should I do if uniapp fails to call a subcomponent method?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template