參考toastr.js 的Vue.js提示/通知框(Toast notification)
使用方式有兩種:
import Toastr from 'vue-toastr';
import 'vue-toastr/dist/vue-toastr.css'
Vue.component('vue-toastr',Toastr);
<vue-toastr ref="toastr"></vue-toastr>
this.$refs.toastr.i("XXXXXX");
import Toastr from 'vue-toastr';
import 'vue-toastr/dist/vue-toastr.css'
Vue.use(Toastr);
//Information
this.$toastr.i("xxxxx");
//Error
this.$toastr.e("xxxxx");
//Success
this.$toastr.s("xxxxx");
//Warning
this.$toastr.w("xxxxx");
以下依據Plugin的使用方式做一些範例。
const toastrConfig = {
defaultTimeout: 2000,
defaultProgressBar: true,
defaultProgressBarValue: 0,
defaultType: "info",
defaultPosition: "toast-top-right",
defaultCloseOnHover: false,
defaultStyle: { "background-color": "red" }
};
Vue.use(Toastr, toastrConfig);
以下是vue-toastr提供的選項,若要使用在全域設定,則加上前綴詞default
。
Prop | Description | Type | Default |
---|---|---|---|
name | 指定該toastr名稱 | String | |
title | Title | String | |
msg | 訊息內容 | String | |
position | 顯示位置,其值可為 toast-top-right ,toast-top-left ,toast-top-full-width , toast-top-center ,toast-bottom-right , toast-bottom-left , toast-bottom-full-width , toast-bottom-center |
String | toast-top-right |
type | info,warning,error,success | String | success |
timeout | 自動N毫秒後關閉 | Number | 5000 |
progressbar | 開啟此選項會於訊息下方加上進度條,需同時至少設定timeout或progressBarValue其一 | Boolean | true |
progressBarValue | 0 - 100 | Number | null |
preventDuplicates | 是否啟用避免重複顯示 | Boolean | false |
style | 樣式(Styles) | Object | {} |
closeOnHover | 當設定為false 時,hover行為將停止timeout引發關閉 |
Boolean | true |
clickClose | 點擊訊息導致關閉 | Boolean | false |
onCreated | Toastr created event | Function | |
onClicked | Click event | Function | |
onClosed | Toastr closed event | Function | |
onMouseOver | Mouse over event | Function | |
onMouseOut | Mouse out event | Function |
name
屬性時,可利用後面提到的API由程式控制關閉this.$toastr.Add({
name: "myInfo",
title: "Demo",
msg: "I overwrite the global options by myself!",
clickClose: false, //Disable click to close
timeout: 3000, // 3 sec
position: "toast-top-full-width",
type: "error", //error, warning, success, info
preventDuplicates: true,
style: { backgroundColor: 'blue',width:'250px' }
});
指定名稱關閉:
this.$toastr.removeByName("myInfo");
指定類型關閉:
this.$refs.toastr.removeByType("error"); // error, warning, success, info
我們可以透過設定選項:closeOnHover=false
以讓使用者Hover到Toastr訊息時能保留訊息在畫面上不關閉。
(移開時,timeout會重新倒數)
this.$toastr.Add({
closeOnHover: false,
timeout: 2000
});