我是靠谱客的博主 英勇大米,这篇文章主要介绍全局组件与局部组件,组件父传子,属性验证&默认属性,组件子传父,组件ref,现在分享给大家,希望可以做个参考。

解释:
1. 父传子 是按 自定义属性 传 比如(:myshow)
2. 子传父 是按 自定义点击事件 传 比如 (@clickshow)


一,全局组件与局部组件

复制代码
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> // 引入vue.js 文件 <script src="../js/vue.js"><script> <style> .app{ margin-top: -10px; margin-left: -10px; } <style> </head> <body> <div class="app"> <search></search> <div> <script> // 全局组件 // component 组件 search 自定义组件名称 Vue.component("search",{ template:`<section style="width: 100vw; height: 30px; background-color: antiquewhite; display: flex; justify-content: space-between; align-items: center;"> <button @click="leftclick"> < </button> <h1>标题{{num}}</h1> <button @clic="reghtclick"> + </button> <local></local> </section>`, methods:{ leftclick(){ console.log("left") }, reghtclick(){ console.log("reght") } }, data(){ return{ num:1111 } }, // 局部组件 component:{ "search":{ template:`<div>我是局部组件</div>` }, methods:{ // 方法 } } }) new Vue(){ el:".app", data(){ return{ } } } <script> </body> </html>

二,组件父传子

复制代码
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../js//vue.js"></script> <style> /* .app{ background-color: antiquewhite; } */ </style> </head> <body> <div class="app"> <thetitle myname="电影" :myshow="false"></thetitle> <br> <thetitle myname="剧场" :myshow="true"></thetitle> </div> <script> Vue.component("thetitle", { // 父传子 // 自定义一个 myname 用于传参 // 自定义一个 myshow 用于显示与隐藏 props:["myname", "myshow"], template: ` <section style="background-color: antiquewhite;"> <button> < </button> <span>{{myname}}</span> <button v-show="myshow"> + </button> </section> ` }) new Vue({ el: ".app", data() { return { } } }) </script> </body> </html>

三,属性验证&默认属性

复制代码
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../js//vue.js"></script> <style> /* .app{ background-color: antiquewhite; } */ </style> </head> <body> <div class="app"> <thetitle myname="电影" :myshow="false"></thetitle> <br> <thetitle myname="剧场" :myshow="false"></thetitle> </div> <script> Vue.component("thetitle", { // 父传子 // 自定义一个 myname 用于传参 // 自定义一个 myshow 用于显示与隐藏 // props:["myname", "myshow"], // 属性验证&默认属性 props:{ myname:{ // type 类型 String 字符串 type:String, // default 默认的 default:'' }, myshow:{ // type 类型 Boolean 布尔 type:Boolean, // default 默认的 default:true } }, template: ` <section style="background-color: antiquewhite;"> <button> < </button> <span>{{myname}}</span> <button v-show="myshow"> + </button> </section> ` }) new Vue({ el: ".app", data() { return { } } }) </script> </body> </html>

四,组件子传父

复制代码
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../js/vue.js"></script> </head> <body> <div class="app"> <navbar @mycustom="myporps"></navbar> <sidebar v-show="inshow"></sidebar> </div> <script> // 标题栏 Vue.component("navbar",{ template: ` <div style="background-color: bisque;"> <button @click="myclick"> ⁜ </button> <span>电影剧场</span> </div> `, methods:{ myclick(){ this.$emit('mycustom',"空轻轻"); } } }) // 侧边栏 Vue.component("sidebar",{ template:` <ul style="width: 200px; height: 800px; background-color: aquamarine;"> <li>1111</li> <li>2222</li> <li>3333</li> <li>4444</li> <li>5555</li> <li>6666</li> </ul> ` }) new Vue({ el:".app", data(){ return{ inshow:true } }, methods:{ myporps(data){ console.log("子传父",data) this.inshow = !this.inshow } } }) </script> </body> </html>

五,组件ref

复制代码
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../js/vue.js"></script> </head> <body> <div class="app"> <button @click="han">$</button> <navbar ref="myid"></navbar> </div> <script> Vue.component("navbar",{ template: `<div> {{myname}} </div>`, data(){ return{ myname:1111111111 } } }) new Vue({ el:".app", data(){ return{ } }, methods:{ han(){ console.log(this.$refs.myid.myname="2222222222") } } }) </script> </body> </html>

最后

以上就是英勇大米最近收集整理的关于全局组件与局部组件,组件父传子,属性验证&默认属性,组件子传父,组件ref的全部内容,更多相关全局组件与局部组件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部