-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvue7.html
More file actions
158 lines (147 loc) · 5.13 KB
/
vue7.html
File metadata and controls
158 lines (147 loc) · 5.13 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Vue 示例</title>
</head>
<body>
<div id="app">
<p>总数 {{ total }}</p>
<!-- <my-component
@increase="handleGetTotal"
@reduce="handleGetTotal"
>
</my-component> -->
<my-component v-model="total">
</my-component>
<button @click="handleReduce"> -1 </button>
<!-- <my-component
@input="handleGetTotal"
>
</my-component> -->
<!-- <child-component>
<p>替代分发内容</p>
<p>替代分发内容</p>
<div slot="footer">底部信息</div>
</child-component> -->
<!-- <child-component>
<template scope="props">
<p>来自父组件的内容</p>
<p>{{ props.msg }}</p>
</template>
</child-component> -->
<child-component :count="1">
</child-component>
<child-component inline-template>
<div>
<h2>在父组件中定义子组件的模板</h2>
<p>{{ message }}</p>
<p>{{ msg }}</p>
</div>
</child-component>
<component :is="currentView"></component>
<button @click="handleChangeView('A')">切换到A</button>
<button @click="handleChangeView('B')">切换到B</button>
<button @click="handleChangeView('C')">切换到C</button>
</div>
<script src="https://unpkg.com/vue/dist/vue.min.js" ></script>
<script>
var data = {
counter: 0
};
Vue.component('my-component', {
props: ['value'],
// template: '<div> <button @click="handleIncrease"> +1 </button> <button @click="handleReduce"> -1 </button></div>',
template: '<input :value="value" @input="updateValue"/>',
data: function(){
return data
// return {
// counter: 0
// }
},
methods: {
handleIncrease: function(){
this.counter++;
// this.$emit('increase', this.counter);
this.$emit('input', this.counter);
},
handleReduce: function() {
this.counter--;
// this.$emit('reduce', this.counter);
this.$emit('input', this.counter);
},
updateValue: function() {
this.$emit('input', event.target.value);
}
}
});
Vue.component('child-component', {
// template: '<div> <slot><p> 显示默认内容 </p></slot> <slot name="footer">默认底部信息</slot><slot msg="来自子组件的内容"/></div> ',
// mounted: function (){
// var footer = this.$slots.footer;
// console.log(footer);
// console.log(footer[0].elm.innerHTML);
// },
// name: 'child-component',
// props: {
// count: {
// type: Number,
// default: 1
// }
// },
// template: '<div class="child"> <child-component :count="count + 1" v-if=" count < 3"></child-component> </div>'
data: function (){
return {
msg: '在子组件声明的数据'
}
}
});
var Home = {
template: '<p> Welcome home! </p>'
};
var app = new Vue({
el: '#app',
data:{
total: 0,
books: [
{name : 'Vue 实战', author: 'rocky'},
{name : 'D JavaScript 实战', author: 'rocky'},
{name : 'C JavaScript 实战1231', author: 'rocky'},
{name : 'B JavaScript 实战12', author: 'rocky'},
{name : 'A JavaScript 实战12', author: 'rocky'},
],
message: '在父组件声明的数据',
currentView: 'comA',
// currentView: Home
},
components: {
comA: { template: '<div>组件A</div>'},
comB: { template: '<div>组件B</div>'},
comC: { template: '<div>组件C</div>'}
},
methods: {
// handleGetTotal: function (total){
// this.total = total;
// }
handleGetTotal: function (total){
this.total = total;
},
handleReduce: function() {
this.total--;
},
handleChangeView: function (component){
this.currentView = 'com' + component;
}
},
computed: {
},
created: function () {
},
mounted: function () {
},
beforeDestory: function () {
},
});
</script>
</body>
</html>