zoukankan      html  css  js  c++  java
  • Vue 使用mixin抽取共通方法

    引入原因:

    • 当一段逻辑在不同的地方使用时

    step-1: 定义mixin文件,methods里有一个handleToLink方法

    /**
     * this mixin file will be used in below places:
     * 1: srcviewshomeaaa.vue
     * 2: srcviewshomebb.vue
     * 3: srcviewsportalccc.vue
     * 4: srcviewsportalddd.vue
     */
    export default {
      methods: {
        // params include 2 fields
        handleToLink(params) {
          console.info('mixin link method')
          this.$store.dispatch('xxxx/xxxxxx').then(() => {
            let user = this.$store.getters.user
            if (user.status == 1) {
              this.$message.warning('xxxxxxxxxx')
              this.$router.push('/logicPage_A')
            } else if (user.status == 2 || user.status == 4) {
              this.$store.dispatch('yyyyyy/yyyyyy').then(flag => {
                if (flag) {
                  this.$router.push({
                    path: '/logicPage_B',
                    query: { xxxxx: xxx yyyyy: yyy }
                  })
                } else {
                  this.$router.push({
                    path: '/logicPage_C',
                    query: { xxxxx: xxx, yyyyy: yyy }
                  })
                }
              })
            } else if (user.status == 3) {
              this.$router.push({
                path: '/logicPage_D',
                query: { xxxxx: xxx, yyyyy: yyy }
              })
            }
          })
        }
      }
    }
    

    step-2: vue文件 引入mixin文件

    import someMixInFile from '@/mixin/someMixInFile'
    export default {
      name: 'yourVueName',
      mixins: [someMixInFile],
      methods: {
        handleForward() {
          // 这里就可以调用混入进来的方法了.
          this.handleToLink(params)
      }
    }
    
    Keep learning
  • 相关阅读:
    [BZOJ1584][Usaco2009 Mar]Cleaning Up 打扫卫生
    CSS浮动
    Django by example -----1总结
    C#函数重载
    linux目录的特点
    Linux调优
    linux
    对齐方式
    19-10-25-G-悲伤
    19-10-24-H
  • 原文地址:https://www.cnblogs.com/leslie1943/p/13364598.html
Copyright © 2011-2022 走看看