当前位置:网站首页>Component communication mode
Component communication mode
2022-06-30 14:35:00 【tutou_ girl】
1. Component communication
adopt prop signal communication ( Father to son )
Subcomponents props Option to receive data from the parent component . you 're right , Only receive ,props Is one-way bound , That is, only the parent component can pass... To the child component , Can't reverse . And there are two ways of transmission :
(1) Static transfer
Subcomponent pass props Option to declare a custom property , Then the parent component can nest the tags , Through this attribute, data is passed to the sub component .
<!-- Parent component -->
<template>
<div>
<h1> I am the parent component !</h1>
<child message=" I am sub component one !"></child> // Pass data through custom attributes
</div>
</template>
<script>
import Child from '../components/child.vue'
export default {
components: {
Child},
}
</script>
<!-- Child components -->
<template>
<h3>{
{
message}}</h3>
</template>
<script>
export default {
props: ['message'] // Declare a custom attribute
}
</script>
(2) Dynamic delivery
We already know that we can give... As above props Pass in a static value , But we need more dynamic data . It can be used at this time v-bind To achieve . adopt v-bind binding props Custom properties of , What you pass is not a static string , It can be an expression 、 Boolean value 、 Objects and so on, any type of value .
<!-- Parent component -->
<template>
<div>
<h1> I am the parent component !</h1>
<child message=" I am sub component one !"></child>
<!-- This is a JavaScript Expression instead of a string .-->
<child v-bind:message="a+b"></child>
<!-- Dynamic assignment with a variable .-->
<child v-bind:message="msg"></child>
</div>
</template>
<script>
import Child from '../components/child.vue'
export default {
components: {
Child},
data() {
return {
a:' I'm subcomponent two !',
b:112233,
msg: ' I'm sub component three !'+ Math.random()
}
}
}
</script>
<!-- Child components -->
<template>
<h3>{
{
message}}</h3>
</template>
<script>
export default {
props: ['message']
}
</script>
adopt ref signal communication ( Father to son )
ref Is used to register reference information for elements or subcomponents . The reference information will be registered in the $refs On the object .$refs It's an object , Hold the registration ref attribute All of the DOM Element and component instances .
If ref Used on subcomponents , Points to component instances , It can be understood as the index of the sub components , adopt $ref It is possible to get the properties and methods defined in the subcomponents .
If ref In ordinary DOM Use on element , The reference points to DOM Elements , adopt $ref It is possible to obtain the DOM Property collection for , Easy access to DOM Elements .
<!-- Parent component -->
<template>
<div>
<h1> I am the parent component !</h1>
<child ref="msg"></child>
</div>
</template>
<script>
import Child from '../components/child.vue'
export default {
components: {
Child},
mounted: function () {
console.log( this.$refs.msg);
this.$refs.msg.setMessage(' I am sub component one !')
}
}
</script>
<!-- Child components -->
<template>
<h3>{
{
message}}</h3>
</template>
<script>
export default {
data(){
return{
message:''
}
},
methods:{
setMessage(m){
this.message=m;
}
}
}
</script>
From the code above, we can see that , adopt ref=‘msg’ Sub components can be child Refer to $ref, And through .msg.setMessage() Call to subcomponent setMessage Method , Pass parameters to subcomponents . The parent component changes the value of the child component by calling the method of the child component
Be careful :
- prop Focus on data transfer , It does not call the properties and methods in the subcomponents . Like when creating article components , Custom use scenarios like titles and content , Most suitable for use prop.
- $ref Focus on indexing , It is mainly used to call properties and methods in sub components , It's not really good at data transfer . and ref Use in dom When the elements , Can make the function of selector , This function is more useful than as an index .
adopt $emit ( Son to father )
Use... Through subcomponents ¥emit Define custom events event And pass the parameters , When this statement is executed to , The parameters will be arg Pass to the parent component , Parent component passed @event Monitor and receive parameters . If the event handler is a method, it will be used as the first parameter , Otherwise you can use $event obtain .


<blog-post @enlarge-text="postFontSize += $event"></blog-post>
边栏推荐
- Fastcgi CGI shallow understanding
- Solution cannot use a scalar value as an array
- When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
- Use PHP to delete the specified text content in the file
- MySQL back to table query optimization
- ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
- Notes on reverse learning in the first week of winter vacation
- PS cutting height 1px, Y-axis tiling background image problem
- PS dynamic drawing
- [buuctf] [actf2020 freshman competition]include
猜你喜欢

Fastcgi CGI shallow understanding
![[geek challenge 2019] PHP problem solving record](/img/bf/038082e8ee1c91eaf6e35add39f760.jpg)
[geek challenge 2019] PHP problem solving record

Go language mutex lock
[email protected][])"/>NoViableAltException([email protected][])

DefCamp Capture the Flag (D-CTF) 2021-22 web

Lifting scanning tool

go time. after

On simple code crawling Youdao translation_ 0's problem (to be solved)

QQ was stolen? The reason is

Initial attack and defense world Misc
随机推荐
Shell programming overview
notepad正则删除关键词所在行
On simple code crawling Youdao translation_ 0's problem (to be solved)
Data recovery software easyrecovery15 Download
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
Summary of use of laravel DCAT admin
Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
LeetCode_ Stack_ Medium_ 227. basic calculator II (without brackets)
Use of laravel repository mode
PHP reverses scenarios based on code and skillfully uses debug_ backtrace()
中信期货开户麻烦吗安全吗,期货开户手续费是多少,能优惠吗
Summary of FTP network protocol learning
JMeter transaction controller
V3_ Chrome extended Chinese translation document V3 directory
Implementation of forwarding server using IO multiplexing
Detailed explanation of the first three passes of upload Labs
Je suis à Foshan, où puis - je ouvrir un compte? L'ouverture d'un compte par téléphone mobile est - elle sécurisée?
JS time conversion standard format, timestamp conversion standard format
Three ways and differences of defining functions in JS