index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <demo-section>
  3. <demo-block :title="$t('title1')">
  4. <van-button @click="$toast($t('text'))">{{ $t('title1') }}</van-button>
  5. <van-button @click="$toast($t('longText'))">{{ $t('longTextButton') }}</van-button>
  6. </demo-block>
  7. <demo-block :title="$t('title2')">
  8. <van-button @click="showLoadingToast">{{ $t('title2') }}</van-button>
  9. </demo-block>
  10. <demo-block :title="$t('title3')">
  11. <van-button @click="showSuccessToast">{{ $t('success') }}</van-button>
  12. <van-button @click="showFailToast">{{ $t('fail') }}</van-button>
  13. </demo-block>
  14. <demo-block :title="$t('advancedUsage')">
  15. <van-button @click="showCustomizedToast">{{ $t('advancedUsage') }}</van-button>
  16. </demo-block>
  17. </demo-section>
  18. </template>
  19. <script>
  20. export default {
  21. i18n: {
  22. 'zh-CN': {
  23. title1: '文字提示',
  24. title2: '加载提示',
  25. title3: '成功/失败提示',
  26. success: '成功提示',
  27. fail: '失败提示',
  28. text: '提示内容',
  29. longText: '这是一条长文字提示,超过一定字数就会换行',
  30. text2: '成功文案',
  31. text3: '失败文案',
  32. text4: second => `倒计时 ${second} 秒`,
  33. longTextButton: '长文字提示'
  34. },
  35. 'en-US': {
  36. title1: 'Text',
  37. title2: 'Loading',
  38. title3: 'Success/Fail',
  39. success: 'Success',
  40. fail: 'Fail',
  41. text: 'Some messages',
  42. longText: 'This is a long message, text will wrap when over a certain length',
  43. text2: 'Success',
  44. text3: 'Fail',
  45. text4: second => `${second} seconds`,
  46. longTextButton: 'Long Text'
  47. }
  48. },
  49. methods: {
  50. showLoadingToast() {
  51. this.$toast.loading({ mask: true, message: this.$t('loading') });
  52. },
  53. showSuccessToast() {
  54. this.$toast.success(this.$t('text2'));
  55. },
  56. showFailToast() {
  57. this.$toast.fail(this.$t('text3'));
  58. },
  59. showCustomizedToast() {
  60. const toast = this.$toast.loading({
  61. duration: 0,
  62. forbidClick: true,
  63. loadingType: 'spinner',
  64. message: this.$t('text4', 3)
  65. });
  66. let second = 3;
  67. const timer = setInterval(() => {
  68. second--;
  69. if (second) {
  70. toast.message = this.$t('text4', second);
  71. } else {
  72. clearInterval(timer);
  73. this.$toast.clear();
  74. }
  75. }, 1000);
  76. }
  77. }
  78. };
  79. </script>
  80. <style lang="less">
  81. .demo-toast {
  82. .van-button {
  83. margin-left: 15px;
  84. }
  85. }
  86. </style>