• 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
  • 相关阅读:
    利用idea里面的mysql插件进行导入sql文件
    JSTL标签
    deepin20系统下配置JAVA开发环境
    deepin20安装及问题解决
    SpringBoot 在项目启之后执行自定义方法的两种方式
    Nick 的经验书
    Java 经验书
    SpringBoot 经验书
    Linux 经验书
    在MacOS中启动SSH服务
  • 原文地址:https://www.cnblogs.com/leslie1943/p/13364598.html
Copyright © 2020-2023  润新知