vue component

vue componet

效果

vue

源码

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<html>
<header>
<title>Vue.js 组件</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</header>

<body>
<div id="box">
<navbar></navbar>
<child v-show="isShow"></child>


</br>
<h2>动态传值 -- 父传子(属性)</h2>
<child :name="myname" :isshow="isShow"></child>
<child :name="myname" :isshow="false"></child>
<child :name="myname" :isshow="true"></child>
<!-- <child :name="myname" isshow="false"></child> -->
<!-- <navbarChild></navbarChild> -->


<br>
<h2>动态传值 -- 子传父(事件)</h2>
//$event 传参数为固定写法
<child @myevent="handlerEvent($event)" name="付款" :isshow="true"></child>



<br>
<h2>example</h2>
<sidebar1 @sidebar-event="handlerSEvent"></sidebar1>
<sidebar2 v-show=isShow></sidebar2>
</div>
</body>

<script>
Vue.component("navbar", {
template: `
<div>
<button @click='handlerClick'>Newer</button>
<button>Older</button>
<child></child>
<navbarChild></navbarChild>
</div>`,
components: {
// 内部组件
navbarChild: { template: `<div><button>navbar child 内部组件</button></div>` }
},
methods: {
handlerClick() {
console.log('narvar')
}
}
})

// 全局组件
Vue.component("child", {
template: `
<div>
<button>Child 全局组件 -- {{name}}</button>
<button v-show="isshow"> isShow-- {{isshow}}</button>
<button v-show="isshow" @click="handlerClick"> 付款 </button>
</div>
`,
// props: ["name", "isshow"],
props: {
name: String,
isshow: Boolean,
t: null // null 表示不限制
},
methods: {
handlerClick() {
console.log(this.count)
this.$emit("myevent", this.count)
}
},
data() {
return {
count: 100
}
}
})


Vue.component('sidebar1', {
template: `<div><button @click="handlerSClick"> 侧边栏 </button></div>`,
methods: {
handlerSClick() {
this.$emit("sidebar-event")
}
}
})

Vue.component('sidebar2', {
template: `<div style="background:red;width:200px">
<ul>
<li> 11111 </li>
<li> 11111 </li>
<li> 11111 </li>
<li> 11111 </li>
<li> 11111 </li>
<li> 11111 </li>
</ul>
</div>`
})

var vm = new Vue({
el: "#box",
data: {
myname: 'XXXX',
isShow: false
},
methods: {
handlerEvent(ev) {
console.log('收到付款:', ev)
},
handlerSEvent() {
this.isShow = !this.isShow
}
}
})
</script>

</html>