uniapp

uniapp父组件怎么向子组件传值

小亿
113
2023-12-23 01:43:13
栏目: 编程语言

在uni-app中,父组件向子组件传值可以通过props属性来实现。

  1. 在父组件中,给子组件添加一个props属性,将要传递的值作为props属性的值。例如:
<template>
  <div>
    <child :message="message"></child>
  </div>
</template>

<script>
  import child from '@/components/child.vue'

  export default {
    components: {
      child
    },
    data() {
      return {
        message: 'Hello, child!'
      }
    }
  }
</script>
  1. 在子组件中,使用props属性接收父组件传过来的值。例如:
<template>
  <div>{{ message }}</div>
</template>

<script>
  export default {
    props: {
      message: {
        type: String,
        default: ''
      }
    }
  }
</script>

这样父组件就可以通过props属性向子组件传递值了。子组件通过props属性接收父组件传递过来的值,并在模板中使用。

0
看了该问题的人还看了