1 emit和on的用法
emit用于调用自定义事件是事件是消费方,on用于指明事件要调用的方法是调度方。
1 |
|
那这个有什么用呢?还不知道,由于叫事件,通常用于全局使用吧,这样能对代码进行切片,降低耦合。
Vue.directive的使用-自定义指令
1 | <html> |
以上demo演示当数据加载时,页面覆盖一层半透明遮蔽层div,相对以往的侵入式,在注册好指令后可以在多个组件进行调用而不用在多个页面上重复地写.
2 Vue.component的用法
新建组件demo
1 | <html> |
3 Vue.extend用法
Vue.extend同上的一样,都是新建组件的。
1 | <html> |
3.2 用Vue.extend封装通用的加载庶罩层
在组件内暴露的调用接口为:
- 使用:
Vue.prototype.loading(:String arg); - 关闭
Vue.prototype.loadiong()()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<html>
<head>
<title>Vue.extend 用法</title>
<meta charset="utf-8"/>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
#loading-wrapper {
position: fixed;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .7);
color: #ffffff;
}
</style>
</head>
<body>
<div id="root">
<button @click="showLoading">显示loading</button>
</div>
<script>
function Loading(msg) {
const LoadingComponent = Vue.extend({
template: '<div id="loading-wrapper">{{msg}}</div>',
props: {
msg: {
type: String,
default: 'loading...'
}
}
}, 'LoadingComponent');
const div = document.createElement('div');
div.setAttribute('id', 'loading-wrapper');
document.body.append(div);
new LoadingComponent({
props: {
msg: {
type: String,
default: msg
}
}
}).$mount('#loading-wrapper');
return () => {
document.body.removeChild(document.getElementById('loading-wrapper'));
}
}
Vue.prototype.$loading = Loading;
new Vue({
el: '#root',
methods: {
showLoading() {
const hide = this.$loading('正在加载中...');
setTimeout(() => {
hide();
}, 2000)
}
}
});
</script>
</body>
</html>
4 Vue.use的用法
1 | <html> |
5 组件通信provide和inject的使用
1 | <html> |
6 过滤器的使用 filter
用于对模板的输出进一步处理,通常用于文本的处理
1 | <html> |
7watch的使用
1 | <html> |
8 class和style的绑定用法
1 | <html> |
9 Vue.observable全局状态管理(Vue 2.6)
"Vue.observable通常用于保存全局状态,类似于Vuex,但更简单。
1 | <html> |
10 slot插槽的使用
1 | <html> |