How to communicate between parent and child components in
Vue? The following article will introduce to you the methods of father to son and son to father. I hope it will be helpful to you!
⭐⭐
## Here we know that the parent component has some data that needs to be displayed by the child component, then we can pass
propsTo complete the communication between components
To complete the communication between components through props
So what are
Props?
means you can register some custom attributes on the component;
script setup,
props You can use the
defineProps() macro to declare:
<script> const props = defineProps(['foo']) console.log(props.foo) </script>
1) The array type
is not used ## In the component of #script setup, prop
can be declared using the props
option: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">export default {
props: ['foo'],
setup(props) {
// setup() 接收 props 作为第一个参数
console.log(props.foo)
}
}</pre><div class="contentsignin">Copy after login</div></div>
Example, use of object syntax
uses components and attributes defined by integer props
<template> <show-info></show-info> </template>
export default { props:{ name :{ type:String, default:"我是默认值name" }, height:{ type:Number, default:2 } } }
String
is declared in the form of object
props (This is quite commonly used)
Use
script setup
defineProps({ title: String, likes: Number })
non-
script setup
export default { props: { title: String, likes: Number } }
3. The child component is passed to the parent component The child component passes content to the parent component:
When some events occur in the child component, such as a click in the component, the parent component needs to switch content;
⭐⭐
Take a counter case
Here we have two sub-components and a parent component
The parent component listens to the addition or subtraction events of the child component, and the parent component listens to the event through the child component
<template> <div> <h2>当前计数:{{counter}}</h2> <!-- 1.自定义add-counter 并且监听内部的add事件 --> <add-counter></add-counter> <!-- 2.自定义su-counter组件,监听内部的sub事件 --> <sub-counter></sub-counter> </div> </template> <script> import AddCounter from './AddCounter.vue' import SubCounter from './SubCounter.vue' export default { components: { AddCounter, SubCounter }, data() { return { counter:0 } }, methods:{ addBtnClick(count) { this.counter += count }, subBtnClick(count) { this.counter -= count } } } </script>
What is defined here is the addition operation of the counter
After the subcomponent event is triggered, through this.$emit The way to emit events
<template> <div> <button>+1</button> <button>+5</button> <button>+10</button> </div> </template> <script> export default { methods:{ btnClick(count) { // 让子组件发出去一个自定义事件 // 第一个参数自定义的事件名称,第二个参数是传递的参数 this.$emit("add",count) } } } </script>
3) Subcomponent 2
SubCounter.vue
What is defined here is the decrement operation of the counter
this.$emit
<template> <div> <button>-1</button> <button>-5</button> <button>-10</button> </div> </template> <script> export default { // 1.emits数组语法 emits:["addd"], methods:{ btnClick(count) { this.$emit("sub",count) } } } </script>
This case is very classic, you can think about it again and again~
(Learning video sharing: Programming Basic video
)The above is the detailed content of A brief analysis of how to communicate between parent and child components in Vue (pass from father to son|pass from son to father). For more information, please follow other related articles on the PHP Chinese website!