首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue将此绑定到导入的规则函数

vue将此绑定到导入的规则函数
EN

Stack Overflow用户
提问于 2020-06-04 14:15:18
回答 3查看 100关注 0票数 1

我有多个Vue文件,它们共享相同的字符串验证检查逻辑,例如:

代码语言:javascript
复制
data(){
        return {
            dialog: false,
            unitEditing:{},
            codeRules:[
                v => !!v || 'can't be empty',
                v => !(/\s/g.test(v)) || 'can't contain space'
            ],
            idRules:[
                v => !!v || 'can't be empty',
                v => !(/\s/g.test(v)) || 'can't contain space',
                v => (this.unitEditing.platform != "unity" || (v=="video" || v=="rewardedVideo")) || "placement for unity should only be video or rewardedVideo"
            ],
        }
    },

我想我可以为此制作一个实用程序文件(../utils/miscUtils.js):

代码语言:javascript
复制
var MiscUtils = {
    rules: {
        codeRules:[
                v => !!v || 'can't be empty',
                v => !(/\s/g.test(v)) || 'can't contain space'
            ],
        idRules:[
                v => !!v || 'can't be empty',
                v => !(/\s/g.test(v)) || 'can't contain space',
                v => (this.unitEditing.platform != "unity" || (v=="video" || v=="rewardedVideo")) || "placement for unity should only be video or rewardedVideo"
            ],
    }
}

export default MiscUtils;

这样我就可以使用:

代码语言:javascript
复制
import MiscUtils from '../utils/miscUtils'
...
data(){
        return {
                dialog: false,
                unitEditing:{},
                codeRules:MiscUtils.rules.codeRules,
                idRules:MiscUtils.rules.idRules
            }
        },

但问题是,我们在检查规则中引用了this.unitEditing。我也试过了

代码语言:javascript
复制
idRules:MiscUtils.rules.idRules.bind(this)

它说MiscUtils.rules.idRules不是一个函数,所以不能绑定。

我如何才能做到这一点?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-04 14:38:54

idRules是一个Array,但您试图对其调用Function#bind (Array没有bind方法)。但是Function#bind不会有帮助,因为箭头函数的上下文已经绑定并且是cannot be re-bound

一种解决方案是使idRules成为一个函数,该函数接受组件实例作为参数并返回Array

代码语言:javascript
复制
var MiscUtils = {
    rules: {
        codeRules: [
                v => !!v || 'can\'t be empty',
                v => !(/\s/g.test(v)) || 'can\'t contain space'
            ],
        idRules: comp => [
                v => !!v || 'can\'t be empty',
                v => !(/\s/g.test(v)) || 'can\'t contain space',
                v => (comp.unitEditing.platform != "unity" || (v=="video" || v=="rewardedVideo")) || "placement for unity should only be video or rewardedVideo"
            ],
    }
}

然后在您的组件中:

代码语言:javascript
复制
data() {
  return {
    dialog: false,
    unitEditing: {},
    codeRules: MiscUtils.rules.codeRules,
    idRules: MiscUtils.rules.idRules(this)
  }
},
票数 1
EN

Stack Overflow用户

发布于 2020-06-04 14:40:54

idRules是一个数组,而不是一个函数;要为各个规则提供this的上下文,您需要将这个misc utils设置为工厂函数,这样您就可以执行以下操作:

代码语言:javascript
复制
var MiscUtils = function () {
  return {
    rules: {
      codeRules: [
        v => !!v || "can't be empty",
        v => !(/\s/g.test(v)) || "can't contain space"
      ],
      idRules: [
        v => !!v || "can't be empty",
        v => !(/\s/g.test(v)) || "can't contain space",
        v => (this.unitEditing.platform != "unity" || (v == "video" || v == "rewardedVideo")) || "placement for unity should only be video or rewardedVideo"
      ]
    }
  }
}

在调用部分:

代码语言:javascript
复制
{
  data() {
    return {
      dialog: false,
      unitEditing: {},
      // Spread the individual rules
      ...MiscUtils.call(this).rules
    }
  }
}

或者,将上下文作为参数传递:

代码语言:javascript
复制
var MiscUtils = context => ({
  rules: {
    codeRules: [
      v => !!v || "can't be empty",
      v => !(/\s/g.test(v)) || "can't contain space"
    ],
    idRules: [
      v => !!v || "can't be empty",
      v => !(/\s/g.test(v)) || "can't contain space",
      v => (context.unitEditing.platform != "unity" || (v == "video" || v == "rewardedVideo")) || "placement for unity should only be video or rewardedVideo"
    ]
  }
})
票数 1
EN

Stack Overflow用户

发布于 2020-06-04 14:36:24

为什么不使用mixins

miscUtils.js:

代码语言:javascript
复制
export default {
    data() {
        return {
            codeRules: [v => !!v || "can't be empty", v => !/\s/g.test(v) || "can't contain space"],
            idRules: [
                v => !!v || "can't be empty",
                v => !/\s/g.test(v) || "can't contain space",
                v =>
                    this.unitEditing.platform != "unity" ||
                    v == "video" ||
                    v == "rewardedVideo" ||
                    "placement for unity should only be video or rewardedVideo"
            ]
        };
    }
};

您可以在您的vue组件中导入此文件并设置混合:

代码语言:javascript
复制
import miscUtils from "FILE_PATH";
export default {
    mixins: [miscUtils],
    data() {
        return {
            dialog: false,
            unitEditing: {}
        };
    },
};

现在你可以使用你的验证和this引用组件实例了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62188002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档