Home  >  Article  >  Web Front-end  >  How do uniapp props call the method of the parent component?

How do uniapp props call the method of the parent component?

PHPz
PHPzOriginal
2023-04-27 09:04:341169browse

With the rapid development of front-end technology, more and more applications require cross-platform development to improve development efficiency and reduce development costs. In this regard, uniapp has become a popular framework. Due to its cross-platform features and convenient development model, many developers choose to use uniapp for development. However, in the process of developing using uniapp, you will encounter many problems, such as how to call the parent component's method through props.

In the vue component, we can transfer data and call methods through parent-child components. uniapp is also based on vue, so we can use the same principle to implement the method of calling the parent component using props in uniapp.

First, we need to define a method in the parent component and handle the logic we need in the method. For example:

<template>
  <div>
    <Child :updateData="updateData"></Child>
  </div>
<template>
<script>
import Child from './Child.vue'

export default {
  components:{
    Child
  },
  methods:{
    updateData(data){
      console.log(data)
    }
  }
}
</script>

We define a method named updateData in the parent component, its function is to process the passed data.

Next, we need to use props in the child component to receive this method so that it can be called when needed.

<template>
  <div>
    <button @click="onClick">调用父组件方法</button>
  </div>
</template>
<script>
export default {
  props: {
    updateData: {
      type: Function,
      required: true
    }
  },
  methods:{
    onClick(){
      // 调用父组件的updateData方法
      this.updateData('Hello World')
    }
  }
}
</script>

In the child component, we use props to receive the updateData method passed by the parent component, and call the onClick method through the @click event. At the same time, we use this.updateData('Hello World') in the onClick method to call The updateData method of the parent component.

Finally, we need to pass data from the parent component to the child component so that the parent component's methods can be called in the child component.

<template>
  <div>
    <Child :updateData="updateData"></Child>
  </div>
<template>
<script>
import Child from './Child.vue'

export default {
  components:{
    Child
  },
  methods:{
    updateData(data){
      console.log(data)
    },
    sendDataToChild(){
      this.$refs.child.onClick()
    }
  },
  mounted(){
    this.sendDataToChild()
  }
}
</script>

In the parent component, we define a sendDataToChild method, whose function is to call the onClick method of the child component through this.$refs.child.onClick(). At the same time, execute the sendDataToChild method in the mounted life cycle hook so that it can be executed automatically after the subcomponent is rendered.

Through the above process, we can use props to call the parent component's method in uniapp. It should be noted that when using props to communicate between parent and child components, we must ensure that the types and values ​​of props are correct, otherwise unexpected errors may occur.

The above is the detailed content of How do uniapp props call the method of the parent component?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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