1
admin
2026-01-28 03ec19e1313fa30b8039f3ce507efb11242dd3ec
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
<!--  -->
<template>
    <div v-if="$store.state.elAlertShow">
        <el-alert :closable="closable" :title="$store.state.elAlertText" :type="$store.state.elAlertType" center
            style="z-index:99999999999999999;position: fixed;top: 0;bottom: 0;margin: auto;">
        </el-alert>
    </div>
 
</template>
 
<script>
export default {
    props: {
        alertShow: {
            type: Boolean,
            default: false
        },
        closable: {
            type: Boolean,
            default: false
        },
        texts: {
            type: String,
            default: ''
        },
        elType: {
            type: String,
            default: 'warning'
        }
    },
    data() {
        return {
            timer: null // 保存定时器引用
        }
    },
    //监听alertShow变成true时,2秒后自动关闭
    watch: {
        // 监听vuex中的elAlertShow变化
        '$store.state.elAlertShow': function (val) {
            // 清除之前的定时器
            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
            if (val) {
                this.timer = setTimeout(() => {
                    this.$store.commit('elAlertShow', { 'elAlertShow': false });
                    this.timer = null;
                }, 2000)
            }
        }
    },
    //生命周期 - 创建完成(访问当前this实例)
    created() {
 
    },
    //生命周期 - 挂载完成(访问DOM元素)
    mounted() {
 
    },
    //生命周期 - 销毁前
    beforeDestroy() {
        // 组件销毁前清除定时器
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
    },
    methods: {
        //定时调用父组件方法关闭弹窗
        closeAlert() {
            //定时调用父组件方法关闭弹窗
            setTimeout(() => {
                this.$emit('closeAlert')
            }, 2000)
        },
 
    }
}
</script>
<style scoped lang="less">
/* @import url(); 引入css类 */
 
 
.tit {
    width: 1rem;
    height: 1rem;
    position: absolute;
    top: 50%;
}
</style>