通知 Notify
全局通知,作为消息提示的增强版,能够自动淡出。
示例
基本形式
<button class="u-btn" on-click={this.show()}>Default</button>
<button class="u-btn" on-click={this.showWithoutTitle()}>Without Title</button>
let component = new RGUI.Component({
template,
show() {
RGUI.Notify.show('This is a message.', 'Notify');
},
showWithoutTitle() {
RGUI.Notify.show('This is a message without title.');
},
});
状态扩展
<button class="u-btn u-btn-info" on-click={this.show('info')}>info</button>
<button class="u-btn u-btn-success" on-click={this.show('success')}>success</button>
<button class="u-btn u-btn-warning" on-click={this.show('warning')}>warning</button>
<button class="u-btn u-btn-error" on-click={this.show('error')}>error</button>
let component = new RGUI.Component({
template,
show(state) {
RGUI.Notify[state](state + ' message', 'Notify');
},
});
位置扩展
<button class="u-btn" on-click={this.show(0)}>top-center</button>
<button class="u-btn" on-click={this.show(1)}>top-left</button>
<button class="u-btn" on-click={this.show(2)}>top-right</button>
<button class="u-btn" on-click={this.show(3)}>bottom-center</button>
<button class="u-btn" on-click={this.show(4)}>bottom-left</button>
<button class="u-btn" on-click={this.show(5)}>bottom-right</button>
let component = new RGUI.Component({
template,
config() {
this.notifies = [
new RGUI.Notify({ data: { position: 'top-center' } }),
new RGUI.Notify({ data: { position: 'top-left' } }),
new RGUI.Notify({ data: { position: 'top-right' } }),
new RGUI.Notify({ data: { position: 'bottom-center' } }),
new RGUI.Notify({ data: { position: 'bottom-left' } }),
new RGUI.Notify({ data: { position: 'bottom-right' } }),
];
},
show(index) {
var notify = this.notifies[index];
notify.show('position: ' + notify.data.position, 'Notify');
},
});
嵌入文档流
上面的notify都是以fixed
的形式固定在浏览器中,也可以将Notify嵌入文档流。先将notify注入到需要的位置,然后设置notify的position="static"
。
<p><button class="u-btn u-btn-primary" on-click={this.show()}>static</button></p>
<notify ref="notify" position="static" duration="0" />
let component = new RGUI.Component({
template,
show() {
this.$refs.notify.show('static notify');
},
});
消息停留时间
可以通过设置notify的duration
参数设置所有消息的停留时间,也可以在show
的时候单独设置该条消息的停留时间,单位为毫秒。
<button class="u-btn" on-click={this.show(500)}>0.5s</button>
<button class="u-btn" on-click={this.show(1000)}>1s</button>
<button class="u-btn" on-click={this.show(2000)}>2s</button>
<button class="u-btn" on-click={this.show(0)}>常驻</button>
let component = new RGUI.Component({
template,
show(duration) {
RGUI.Notify.show('duration: ' + duration + ' ms', 'Notify', duration);
},
});
始终显示一条
将single
设置为true
,可以让notify始终只显示一条消息。
<button class="u-btn u-btn-info" on-click={this.show('info')}>Info</button>
<button class="u-btn u-btn-success" on-click={this.show('success')}>Success</button>
<button class="u-btn u-btn-warning" on-click={this.show('warning')}>Warning</button>
<button class="u-btn u-btn-error" on-click={this.show('error')}>Error</button>
let component = new RGUI.Component({
template,
config() {
this.notify = new RGUI.Notify({ data: {single: true} });
},
number: 1,
show: function(state) {
this.notify[state]('message ' + this.number, 'Notify');
this.number++;
}
});
API
Class
Notify
继承自。
Options
new Notify()
参数 | 类型 | 默认值 | 数据流向 | 描述 |
---|---|---|---|---|
data | Object | 绑定属性 | ||
data.position | string | 'top-right' | outside => inside | 通知的位置,可选参数:`top-center`、`top-left`、`top-right`、`bottom-center`、`bottom-left`、`bottom-right`、`static` |
data.duration | number | 3000 | outside => inside | 每条通知默认的停留毫秒数,如果为0,则表示通知常驻不消失。 |
data.single | boolean | false | outside => inside | 是否始终显示一条 |
data.visible | boolean | true | outside => inside | 是否显示 |
data.class | string | '' | outside => inside | 补充class |
Methods
notify.show(content[, title][, duration][, state])
弹出一个通知
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
content | string | '' | 通知内容 |
title | string | '' | 通知标题 |
duration | number | notify.duration | 该条通知的停留毫秒数。如果为0,则表示通知常驻不消失。如果不填,则使用notify默认的duration。 |
state | string | '' | 通知状态,可选参数:`info`、`success`、`warning`、`error` |
返回值 | 类型 | 描述 | |
item | object | 返回弹出的通知项 |
notify.close(item)
关闭某条通知
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
item | object | 需要关闭的通知项 | |
返回值 | 类型 | 描述 | |
无返回值 |
notify.closeAll()
关闭所有通知
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
无参数 | |||
返回值 | 类型 | 描述 | |
无返回值 |
notify.[info|success|warning|error](content[, title][, duration])
弹出特殊类型的通知。为show方法的简写方式。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
content | string | '' | 通知内容 |
title | string | '' | 通知标题 |
duration | number | toast.duration | 该条通知的停留毫秒数。如果为0,则表示通知常驻不消失。如果不填,则使用toast默认的duration。 |
返回值 | 类型 | 描述 | |
无返回值 |