补充

全局单例公共组件

1
2
3
message
- index.ts
- Message.vue

index.ts

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
import Message from "./Message.vue";

export type MessageType = "default" | "success" | "error" | "warning";

export interface MessageOptions {
type?: MessageType;
time?: number;
callback?: () => void;
}

// 单例模式
let vm;

const install = (Vue) => {
let Component = Vue.extend(Message);

Vue.prototype.$message = (
content: String,
options?: MessageType | MessageOptions
) => {
let data = {
content,
};

if (vm) {
// 全局只保持一个 Message 公共组件
vm.$destroy();
}

if (typeof options === "string") {
data["type"] = options;
} else {
Object.assign(data, options);
}

let instance = new Component({
data,
});

vm = instance.$mount();
vm.show = true;

vm.$on("destroyed", () => {
vm = null;
});

return vm;
};
};

export default {
install,
Message,
};

Message.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
<template>
<transition name="slide" @after-leave="afterLeave">
<div v-if="show" :class="component_class" @click="show = false">
{{ content }}
</div>
</transition>
</template>

<style lang="less" scoped>
.message {
position: fixed;
bottom: 60px;
left: 50%;
font-size: 14px;
color: #333;
padding: 5px 10px;
text-align: center;
overflow: hidden;
opacity: 0.9;
border-radius: 4px;
min-width: 60px;
max-width: 60%;
word-break: break-all;
box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, 0.16);
transform: translate(-50%, 0);
z-index: 10001;

&.default {
color: #fff;
background-color: #333;
}

&.success {
color: #fff;
background-color: @success;
}

&.warning {
color: #fff;
background-color: @warning;
}

&.error {
color: #fff;
background-color: @error;
}
}

.slide-enter-active,
.slide-leave-active {
transition: all 0.3s;
}
.slide-enter,
.slide-leave-to {
transform: translate(-50%, 10px);
opacity: 0;
}
</style>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";

@Component({})
export default class Toast extends Vue {
show = false;
content = "";
time = 2000;
type = "default";
callback = null;

get component_class() {
return ["message", `${this.type}`];
}

mounted() {
document.body.appendChild(this.$el);
this.time && setTimeout(this.close, this.time);
}

beforeDestroy() {
document.body.removeChild(this.$el);
this.callback &&
this.$nextTick(() => {
this.callback();
});
}

destroyed() {
this.$emit("destroyed");
}

close() {
this.show = false;
}

afterLeave() {
this.$destroy();
}
}
</script>