1、父傳值給子組件
父組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<template> <div> <p class= "father" >父組件</p> <child :sid= "id" ></child> </div> </template> <script> import child from './child' export default { components: { child }, data() { return { id: '1234' // 父組件向子組件傳的值 } } } </script> |
子組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<template> <div> <p class= "child" >子組件</p> <p class= "child" >接收父組件的值是:{{ sid }}</p> </div> </template> <script> export default { props:{ sid:{ type:String, default : '0' } }, // props:["sid"], data() { return { } } } </script> |
說明:
①sid是在子組件中綁定要傳的值,記住“=”前的sid要和子組件中要接受的變量名要一致
②在子組件中用props來接受傳入的值,可以寫成對象類型,規定類型和默認值,也可以直接寫成字符串
③在子組件中可以直接使用,也可以在函數中使用this.sid進行訪問
2、子傳值給父組件
父組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<template> <div> <p class= "father" >父組件</p> <p class= "father" >接收到子組件的值:{{childSid}}</p> <child @passVaule= "parentPassValue" ></child> </div> </template> <script> import child from './child' export default { components: { child }, data() { return { childSid: '' // 接收子組件的值 } }, methods: { parentPassValue(data) { this .childSid = data; } } } </script> |
子組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<template> <div> <p class= "child" >子組件</p> <button @click= "valueClick" >傳值</button> </div> </template> <script> export default { data() { return { } }, methods: { valueClick() { this .$emit( 'passVaule' ,19) } } } </script> |
說明:
①子組件中給一個方法去觸發$emit,第一個參數是在父組件引入子組件綁定的函數名('passVaule'),第二個是要傳的值(19)
②父組件中綁定一個函數,調用父組件中綁定的函數,在其中對值進行接收操作
3、子調用父組件中的方法
父組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<template> <div> <p class= "father" >父組件</p> <child @funVaule= "parentFunValue" ></child> </div> </template> <script> import child from './child' export default { components: { child }, data() { return { } }, methods: { parentFunValue() { console.log( '調用了父組件中的函數' ); } } } </script> |
子組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<template> <div> <p class= "child" >子組件</p> <button @click= "funClick" >調用父組件方法</button> </div> </template> <script> export default { data() { return { } }, methods: { funClick() { this .$emit( 'funVaule' ) } } } </script> |
說明:
①這個和子傳值給父類似,只是不傳值,調用了父組件的綁定的函數
4、父調用子組件中的方法
父組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<template> <div> <p class= "father" >父組件</p> <button @click= "childClick" >調用子組件方法</button> <child ref= "mychild" ></child> </div> </template> <script> import child from './child' export default { components: { child }, data() { return { } }, methods: { childClick() { this .$refs.mychild.testNum(1) } } } </script> |
子組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<template> <div> <p class= "child" >子組件</p> </button> </div> </template> <script> export default { data() { return { } }, methods: { testNum(data) { console.log( '調用了子組件中的方法:' , data); } } } </script> |
說明:
① 父組件中在引入的子組件中寫入 ref = "mychild" mychid為自己定義的實例名
② 在函數中寫 this.refs.mychild.testNum(), “testNum”為子組件中定義的函數名
③子組件定義一個函數,讓父組件調用即可
④這個方法也可以進行傳值,在括號中傳入值,子組件接收即可
以上就是vue父子組件的互相傳值和調用的詳細內容,更多關于vue父子組件的傳值和調用的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/louis-liu-oneself/p/13388065.html