我是靠谱客的博主 坚定小馒头,这篇文章主要介绍【Vue】 使用vuex 封装一个toast 组件,现在分享给大家,希望可以做个参考。

使用vuex进行状态管理,使用css控制toast的显示。

type表示toast中的图标,可以自定义添加多种图标,例子中只设置了normal与success,

通过showToast控制toast的css的fadin和fadeout类,通过css的动画效果实现toast逐渐消失的效果

复制代码
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
//toast.components <template> <div class="toast-wrapper" :class="{ 'fadein': showToast,'fadeout': !showToast}"> <div class="icon" v-if="toastInfo.type!=='normal'" :class="toastInfo.type" > </div> <div class="message">{{toastInfo.message}}</div> </div> </template> <script> import { mapGetters } from 'vuex'; export default { name: "Toast", data() { return { message: "", type: "normal" // noraml 默认,无图标; success 有图标 }; }, computed:{ ...mapGetters(['showToast','toastInfo']) }, }; </script> <style lang="less" scoped> @Medium: PingFangSC-Medium, "Microsoft YaHei"; @Regular: PingFangSC-Regular, "Microsoft YaHei"; @Primary: #ff7400; .toast-wrapper { position: absolute; left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); width: 7.07rem; height: 5.21rem; background: rgba(0, 0, 0, 0.8); border-radius: 4px; display: flex; flex-direction: column; justify-content: center; align-items: center; &.fadein { opacity: 0.8; transition: opacity ease-in 0s; } &.fadeout { opacity: 0; transition: all ease-out 0.5s; } .icon{ display: block; } .success{ width: 1.29rem; height: 1.29rem; margin-bottom: 0.43rem; background: url('../assets/success.png'); background-size: 1.29rem 1.29rem; } .message{ font-family: @Regular; font-size: 15px; text-align: center; color: #fff; } } </style>

通过vuex管理toast的toastInfo,即message和time以及type

复制代码
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
// index.js import mutations from './mutations.js'; import actions from './actions.js'; import getters from './getters.js'; const state = { showToast: false, toastInfo: { type: 'normal', meassage: '' } }; export default { state, getters, actions, mutations }; // getter.js export default { showToast: state => state.showToast, toastInfo: state => state.toastInfo }; // actions.js export default { setToast ({ dispatch, commit }, payload) { commit('updateToast', true); commit('updateToastInfo', payload); setTimeout(() => { dispatch('delToast'); }, payload.time); }, delToast ({ commit }) { commit('updateToast', false); } }; // mutations.js export default { updateToast (state, params) { state.showToast = params; }, updateToastInfo (state, params) { state.toastInfo = { ...params }; } };

最后在组件里使用, 注意,因为是通过vuex控制toasShow,然后在toasShow为false的时候,toast还有0.5s的时间逐渐隐藏,所以整体展示的时间是settoast中的time时间加0.5s

复制代码
1
2
3
4
5
6
7
8
import { mapGetters, mapActions } from 'vuex'; methods: { ...mapActions([ 'setToast']), someMedthod() { this.setToast({time: 1500, message: '成功',type: 'success'}); } }

 

最后

以上就是坚定小馒头最近收集整理的关于【Vue】 使用vuex 封装一个toast 组件的全部内容,更多相关【Vue】内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部