vue 进阶系列之全局API


vue 进阶系列之指令后,再写 vue 进阶系列之全局 API

  • 基于一个简单的TodoList来演示全局API的使用, 在使用到全局API的地方会有注释标明
  • 请仔细阅读注释处所涉及到的api的使用方法
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> vue 进阶系列之全局 API 的使用</title>
<style>
.todo-list {
margin: 100px auto;
width: 500px;
text-align: center;
font-family: '微软雅黑';
}

.display-item li{
text-align: left;
padding-left: 1rem;
color: #bbbbbb;
}

.input-todo-item>input {
width: 300px;
height: 35px;
border: 1px solid #bbb;
border-radius: 4px;
padding: 0 1rem;
}

.input-todo-item>input:focus {
border-radius: 4px;
}

.input-todo-item>button {
line-height: 35px;
color: #ffffff;
background-color: red;
outline: none;
opacity: .6;
border-radius: 4px;
border: none;
padding: 0 1rem;
cursor: pointer;
}

.input-todo-item>button:hover {
opacity: 1;
}

.disableBtn {
background-color: #bbb !important;
opacity: 1 !important;
}

.samll-widget {
display: inline-block;
height: 20px;
width: 30px;
line-height: 20px;
text-align: center;
color: #ffffff;
transition: all 1s;
/* border: 1px solid #eee; */
border-radius: 2px;
margin-left: 1rem;
opacity: .5;
cursor: pointer;
}

.samll-widget:hover {
opacity: 1;
}

</style>
</head>

<body>
<div id="mount-point">
<div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
// 定义 <input-item/>组件 html 片段
const inputTpl =
`<div class="input-todo-item">
<input v-focus type="text" v-model="inputValue"/>
<button
ref="addbtn"
@click="addClick"
:disabled="disableAddBtn"
:class="{disableBtn: disableAddBtn}">
{{ add | capitalize}}
</button>
</div>
`
// 定义 <display-item>组件 html 片段
const disTpl =
`<div class="display-item">
<ul>
<li
v-for="(item, index) in disList"
:key="index"
:style="{textDecorationLine: item.isCompleted ? 'line-through' : 'none'}">
{{item.text}}
<small-widget type="delete" index="index" @onClickWidget="deleteItem(index)">
<template v-slot:widget>x</template>
</small-widget>
<small-widget type="finished" index="index">
<template v-slot:widget>o</template>
</small-widget>
</li>
</ul>
</div>`
// 定义 TodoList html 片段
const tpl =
`<div class="todo-list">
<input-item @onAddItem="addItemToList"/>
<display-item :disList="todoLists"/>
</div>`
/** global API
* @Vue.compile(template)
* 功能: 在 render 函数中编译模板字符串
* params:
*/
const widget = Vue.compile(`<span ref="small-widget" class="samll-widget" @click="handleClick"><slot name="widget"></slot></span>`)
Vue.component('small-widget', {
props: {
type: {
type: String
},
index: {
type: String
}
},
computed: {
color () {
if (this.type === 'delete') {
return 'red'
} else if (this.type === 'remind') {
return 'yellow'
} else {
return 'green'
}
}
},
mounted () {
this.$refs['small-widget'].style.backgroundColor = this.color
},
methods: {
handleClick () {
this.$emit('onClickWidget')
// console.log('fsafasfsafsafa' ,this.$parent.$parent.$data)
}
},
render: widget.render
})
/** global API
* @Vue.extend(options)
* 功能: 使用基础 Vue 构造器,创建一个“子类”
* params: 一个包含组件选项的对象。
*/
const DisplayItem = Vue.extend({
template: disTpl,
props: {
disList: {
type: Array
}
},
methods: {
deleteItem(index) {
this.$emit('onDeleteItem', index)
}
}
})
/** global API
* @Vue.component(id, [definition])
* 功能: 注册或获取全局组件
* params: {string} id, {Function | Object} [definition]
*/
// 使用构造器创建组件
Vue.component('display-item', DisplayItem)
// 使用模板选项创建组件
Vue.component('input-item', {
template: inputTpl,
data() {
return {
inputValue: '',
add: 'add'
}
},
computed: {
disableAddBtn() {
return !this.inputValue
}
},
methods: {
addClick() {
console.log(this.disableAddBtn)
if (this.disableAddBtn) return
this.$emit('onAddItem', this.inputValue) // 实例方法/事件
}
}
})
// 使用render函数创建组件
Vue.component('todo-list', {
render: function (createElement) {
return createElement('div', {'class': {'todo-list': true}}, [
createElement('input-item', {
on: {
onAddItem: this.$parent.addItemToList
},
}, this.$slots.default),
createElement('display-item', {
props: {
disList: this.$parent.$data.todoLists
},
on: {
onDeleteItem: this.$parent.deleteItemFromList
},
}, this.$slots.default),
])
}
})
const TodoList = new Vue({
props: {
data: {
type: Object
}
},
data() {
return {
todoLists: [
{
text: '这个周末去song'
}
]
}
},
methods: {
deleteItemFromList (index) {
this.todoLists.splice(index, 1)
}
},
render: function (createElement) {
return createElement('todo-list')
}
})
/** global API
* @Vue.mixin(id, [definition])
* 功能: 定义一个全局混入
* params: {string} id {Function | Object} [definition]
*/
Vue.mixin({
created: function () {
var myOption = this.$options.myOption
console.log('[myOption]', myOption)
}
})
/** global API
* @Vue.directive(id, [definition])
* 功能: 注册或获取一个全局指令
* params: {string} id {Function | Object} [definition]
*/
Vue.directive('focus', {
inserted (el, binding, vNode) {
el.focus()
}
})
/** global API
* @Vue.vue(plugin)
* 功能: 安装 Vue.js 插件
* params: {Object | Function} plugin
*/
Vue.use({
install(Vue) {
let message = (context, options) => {
let res = Vue.compile('<div>{{options.text}}</div>')
let Message = Vue.extend({
render: res.render
})
new Message().$mount(context)
}
Vue.prototype.$message = message
}
})
/** global API
* @Vue.filter(id, [definition])
* 功能: 注册或获取全局过滤器。
* params: {string} id {Function | Object} [definition]
*/
Vue.filter('capitalize', function (value) {
if (!value) return ''
value = value.toString()
return value.charAt(0).toUpperCase() + value.slice(1)
})
/** global API
* @Vue.set(target, propertyName/index, value )
* 功能: 向响应式对象中添加一个属性
* params: 一个包含组件选项的对象。
*/
Vue.set(TodoList, 'key', {
text: '这个周末去song'
})
let list = TodoList.$data.todoLists
list = list.map(item => {
TodoList.$set(item, 'isCompleted', true)
return item
})
TodoList.addItemToList = value => {
list.push({
isCompleted: false,
text: value
})
TodoList.$data.todoLists = list
}
TodoList.$mount('#mount-point')
</script>
</body>

</html>