vue -提示框组件的封装

旧城等待, 2021-08-13 22:50 606阅读 0赞

1、效果图:

在这里插入图片描述

2、子组件的封装

  1. <template>
  2. <div class="toast" v-if="toastStatus">
  3. <span class="desc">{
  4. {tips}}</span>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "",
  10. data() {
  11. return {
  12. timers: 3000,
  13. toastStatus:false
  14. };
  15. },
  16. props: {
  17. timer: Number,
  18. status: Boolean,
  19. tips: String
  20. },
  21. watch:{
  22. status(val){
  23. this.toastStatus = val;
  24. if(val){
  25. this.changeStatus();
  26. }
  27. }
  28. },
  29. methods: {
  30. changeStatus() {
  31. let _this = this;
  32. _this.timers = _this.timer;
  33. setTimeout(function() {
  34. _this.toastStatus = false;
  35. _this.$emit('clearTimer',false);
  36. }, _this.timers);
  37. }
  38. }
  39. };
  40. </script>
  41. <style lang="css" scoped>
  42. .toast {
  43. position: fixed;
  44. top: 40%;
  45. left: 43%;
  46. }
  47. .toast .desc {
  48. width: 60%;
  49. font-size: 20px;
  50. color: #fff;
  51. padding: 18px 24px;
  52. background: rgba(0, 0, 0, 0.51);
  53. border-radius: 15px;
  54. }
  55. </style>

3、使用方式

  1. <template>
  2. <div class="login">
  3. // 使用
  4. <Toast :status="toastStatus" :tips="tips" @clearTimer="clearTimer" :timer="timer"></Toast>
  5. </div>
  6. </template>
  7. <script>
  8. // 引入
  9. import Toast from "../components/toast";
  10. export default {
  11. data() {
  12. return {
  13. toastStatus: false, // 提示框
  14. timer:3000,
  15. tips: "请输入fgdgjdljkljkl账号"
  16. };
  17. },
  18. components: {
  19. Toast
  20. },
  21. methods: {
  22. clearTimer(val){
  23. this.toastStatus = val;
  24. }
  25. }
  26. };
  27. </script>

发表评论

表情:
评论列表 (有 0 条评论,606人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue封装组件过程

    vue组件的定义 ● 组件(Component)是Vue.js最强大的功能之一 ● 组件可以扩展HTML元素,封装可重用代码 ● 在较高层面上,组件是自定义元素,Vue.

    相关 vue - 搜索组件封装及使用

      在写一个项目的时候,我们在多个页面会使用到搜索功能,这时我们一般都会选择直接写一个组件给多个页面一起使用,下面我给大家分享一下我的组件封装,如果不妥之处,请多多指教!