vue中怎么用props接收参数
时间: 2023-06-15 12:02:55 浏览: 43
在 Vue 中,可以使用 `props` 属性来接收父组件传递的数据。在子组件中,可以通过 `props` 属性来声明需要接收的数据,然后在父组件中通过绑定属性的方式将数据传递给子组件。
以下是一个简单的例子:
```html
<!-- 父组件 -->
<template>
<div>
<child-component :message="hello"></child-component>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
components: {
ChildComponent
},
data() {
return {
hello: 'Hello, World!'
}
}
}
</script>
```
在上面的例子中,父组件通过 `:message="hello"` 将 `hello` 数据传递给了子组件。
```html
<!-- 子组件 -->
<template>
<div>
{{ message }}
</div>
</template>
<script>
export default {
props: ['message']
}
</script>
```
在子组件中,通过 `props: ['message']` 声明了需要接收 `message` 数据,并通过 `{{ message }}` 的方式在模板中使用了这个数据。这样,父组件传递的 `hello` 数据就可以在子组件中被使用了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)