这里总结了一个简单的实现全局消息通知的方法,实现的效果如下图:
1、修改/src/App.vue
文件,中增加消息控件,放在<router-view/>
后面
<div class="ant-message">
<div class="ant-content">
<div class="ant-message-notice"
:class="[type=='success'?'ant-message-success':'',
type=='error'?'ant-message-error':'',
type=='info'?'ant-message-info':'',
type=='warning'?'ant-message-warning':'']">
<span>{{message}}</span>
</div>
</div>
</div>
2、同样在/src/App.vue
文件中,在
.ant-message{
position: fixed;
z-index: -1;
width:100%;
top: 51px;
}
.ant-content{
font-family: "Helvetica Neue For Number", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.5;
color: rgba(0, 0, 0, 0.65);
-webkit-box-sizing: border-box;
box-sizing: border-box;
pointer-events: none;
display: inline-block;
}
.ant-message-notice{
padding:16px 30px;
font-size: 16px;
color:black;
border-radius: 4px;
box-shadow:0 4px 12px rgba(0,0,0,0.15);
background: white;
display: none;
pointer-events: all;
box-sizing:border-box;
}
.ant-message-error:before{
content:"\f057";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
font-size: 18px;
margin-right:5px;
z-index: 3;
color:red;
}
.ant-message-info:before{
content:"\f05a";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
font-size: 18px;
margin-right:5px;
z-index: 3;
color: #4d7dff;
}
.ant-message-warning:before{
content:"\f071";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
font-size: 18px;
margin-right:5px;
z-index: 3;
color: #ffdd65;
}
.ant-message-success:before{
content:"\f058";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
font-size: 18px;
margin-right:5px;
z-index: 3;
color: #3da638;
}
3、 在src/main.js中增加全局函数($message):
Vue.prototype.$message = function(option){
$('.ant-message-notice span').html(option.text)
$('.ant-message-notice').addClass('ant-message-'+option.type)
$('.ant-message').css('z-index',9999)
$ ('.ant-message-notice').show ().delay (3000).fadeOut (function () {
$('.ant-message').css('z-index',-1)
});
}
4、在需要弹出消息提示的时候使用this.$message({"type": "warning", "text": "message"})
即可