zoukankan      html  css  js  c++  java
  • vue编写以plugin形式调用的插件——以全局错误展示界面为例

    应用场景:

    使用vux的小伙伴应该能体会到,以插件形式调用的toast、loading用起来都停不错的,只需要main.js中添加为vue的实例方法,就可以在页面中根据需要随时随地的进行调用。

    同样,项目中也需要一些公用的页面,如全局的访问异常页面。如果采用普通的组件引用,则需要在每个需要用到的页面中进行引用,就比较麻烦了。

    最好的就是做成像toast插件一样能直接调用的插件,是最好的解决方法。

    示例代码:

    components/error.vue

     1 <template>
     2   <div class="error tc" v-show="show">
     3     <Icon class="icon c_c" type="info" is-msg></Icon>
     4     <p class='msg f16 c_6' :style="{'width':width}">{{msg||"访问出错,请返回重试!"}}</p>
     5     <button @click='goToIndex' size='mini'>返回首页</button>
     6   </div>
     7 </template>
     8 
     9 <script>
    10   import { Icon } from 'vux'
    11   export default {
    12     components: {
    13       Icon
    14     },
    15     props: {
    16       msg: String,
    17       show: Boolean,
    18        String //百分比
    19     }
    20     ......
    21   }
    22 </script>
    23 <style scoped>
    24   .error {
    25     width: 100%;
    26     height: 100%;
    27     overflow: hidden;
    28     position: fixed;
    29     top: 0;
    30     left: 0;
    31     z-index: 9999;
    32   }
    33   
    34   .icon {
    35     margin: 1.2rem auto .4rem auto;
    36     font-size: 70px!important;
    37   }
    38   
    39   button {
    40     margin-top: 1rem;
    41   }
    42   
    43   .msg {
    44     margin: 0 auto;
    45     width: 60%;
    46   }
    47 </style>

    注:width参数用于自定义设置错误信息展示的宽度(默认宽度为60%,可以通过width参数进行自定义)。

    components/error.js

     1 import errorComp from './error.vue'
     2 
     3 const error = {};
     4 
     5 // 注册Toast
     6 error.install = function(Vue) {
     7   // 生成一个Vue的子类
     8   // 同时这个子类也就是组件
     9   const ErrorConstructor = Vue.extend(errorComp)
    10   // 生成一个该子类的实例
    11   const instance = new ErrorConstructor();
    12 
    13   // 将这个实例挂载在我创建的div上
    14   // 并将此div加入全局挂载点内部
    15   instance.$mount(document.createElement('div'))
    16   document.body.appendChild(instance.$el)
    17 
    18   // 通过Vue的原型注册一个方法
    19   // 让所有实例共享这个方法 
    20   Vue.prototype.$error = (msg) => {
    21     instance.msg = msg;
    22     instance.show = true;
    23   }
    24   //隐藏并重置插件
    25   Vue.prototype.$errorHide = () => {
    26     instance.show = false;
    27     instance.msg = '';
    28   }
    29 }
    30 
    31 export default error

    页面引用:

        beforeDestroy(){
          //关闭并重置错误展示插件
          this.$errorHide()
        },
        created() {
          //必要参数缺失,则展示错误页面
          if(!this.$route.params.coupon_id) {
            this.$error('当前访问错误,请稍后重试','55%')//根据需要传入错误提示信息
          }
        ......

    如上,如果是一般的异常,直接使用默认错误提示即可,无需传参。如需要自定义错误信息及宽度,传入相应参数即可。

    另外,由于是全局注入的插件,需要在当前页面销毁的时候进行关闭。

  • 相关阅读:
    058_从键盘读取一个论坛积分,判断论坛用户等级
    057_统计 Linux 进程相关数量信息
    bzoj3436
    bzoj1202
    bzoj1044
    bzoj2338
    bzoj1854
    bzoj1856
    830C
    bzoj2132
  • 原文地址:https://www.cnblogs.com/xyyt/p/13141249.html
Copyright © 2011-2022 走看看