【问题标题】:Webpack class is not sharing the this from the constructor to the class's methods [duplicate]Webpack 类未将 this 从构造函数共享到类的方法 [重复]
【发布时间】:2018-12-27 09:43:37
【问题描述】:

我正在尝试使用 Webpack 和 JS 定义一个类。 在构造函数中,我可以访问数据,但是在将数据分配给this.t 之后,this.t 在类中的未来方法中没有数据。这是课程:

module.exports = class JoiCustomErrors {

  constructor(t) {
    this.t = t;
    console.log(this.t); // the wanted object info
  }

  parser(errors) {
    console.log(this.t); // undefined
    return errors.map(err => {
      err.message = ({
        "string.base": `${this.t['This field is required']}`,
        "string.min": `${this.t['Minimum']} ${err.context.limit} ${this.t['chars']}`,
        "string.max": `${this.t['Maximum']} ${err.context.limit} ${this.t['chars']}`,
        "any.empty": `${this.t['This field is required']}`,
        "any.required": `${this.t['This field is required']}`,
        "number.min": `${this.t['Should be greater than']} ${err.context.limit}`,
        "number.max": `${this.t['Should be less than']} ${err.context.limit}`,
        "number.base": `${this.t['Digits only']}`,
        "date.base": `${this.t['Date format must be']} yyyy-mm-dd`,
        "date.isoDate": `${this.t['Date format must be']} yyyy-mm-dd`,
        "date.min": `${this.t['Should be later than']} ${err.context.limit}`,
        "date.max": `${this.t['Should be earlier than']} ${err.context.limit}`,
        "any.allowOnly": `${this.t['Unknown value']}`,
        "array.max": `${this.t['Maximum']} ${err.context.limit} ${this.t['Items']}`,
        "array.min": `${this.t['Minimum']} ${err.context.limit} ${this.t['Items']}`,
        "array.includesRequiredUnknowns": `${this.t['Minimum']} 1 ${this.t['Items']}`,
      })[err.type];

      if (err.type=="string.regex.name") {
        if (err.context.name=="alphabeta") err.message = `${this.t['Letters only']}`;
        if (err.context.name=="alphanum") err.message = `${this.t['Letters and digits only']}`;
        if (err.context.name=="num") err.message = `${this.t['Digits only']}`;
        if (err.context.name=="latin") err.message = `${this.t['Entered invalid chars']}`;
        if (err.context.name=="username") err.message = `${this.t['Letters and digits only in english']}`;
      }

      return err;
    });
  }

}

我是这样设置的:

let jce = new JoiCustomErrors($rootScope.t);
      let schema = Joi.object().keys({
        first_name: Joi.string().min(2).max(5).required().error(jce.parser)
      });
      Joi.validate({first_name: "a"}, schema, { abortEarly: false }, err => {
        console.log(err);
        return err;
      });

以上代码将错误发送到JoiCustomErrors类中的解析器方法。

但是在实例声明中声明的 $rootScope.t 是不可访问的。

如何解决这个问题?

【问题讨论】:

    标签: javascript webpack ecmascript-6


    【解决方案1】:

    在类构造函数中绑定解析器方法,在类方法内访问类的this

    module.exports = class JoiCustomErrors {
    
      constructor(t) {
        this.t = t;
        this.parser = this.parser.bind(this);
      }
    
      parser(errors) {
        console.log(this.t);
        return errors.map(err => {
          err.message = ({
            "string.base": `${this.t['This field is required']}`,
            "string.min": `${this.t['Minimum']} ${err.context.limit} ${this.t['chars']}`,
            "string.max": `${this.t['Maximum']} ${err.context.limit} ${this.t['chars']}`,
            "any.empty": `${this.t['This field is required']}`,
            "any.required": `${this.t['This field is required']}`,
            "number.min": `${this.t['Should be greater than']} ${err.context.limit}`,
            "number.max": `${this.t['Should be less than']} ${err.context.limit}`,
            "number.base": `${this.t['Digits only']}`,
            "date.base": `${this.t['Date format must be']} yyyy-mm-dd`,
            "date.isoDate": `${this.t['Date format must be']} yyyy-mm-dd`,
            "date.min": `${this.t['Should be later than']} ${err.context.limit}`,
            "date.max": `${this.t['Should be earlier than']} ${err.context.limit}`,
            "any.allowOnly": `${this.t['Unknown value']}`,
            "array.max": `${this.t['Maximum']} ${err.context.limit} ${this.t['Items']}`,
            "array.min": `${this.t['Minimum']} ${err.context.limit} ${this.t['Items']}`,
            "array.includesRequiredUnknowns": `${this.t['Minimum']} 1 ${this.t['Items']}`,
          })[err.type];
    
          if (err.type=="string.regex.name") {
            if (err.context.name=="alphabeta") err.message = `${this.t['Letters only']}`;
            if (err.context.name=="alphanum") err.message = `${this.t['Letters and digits only']}`;
            if (err.context.name=="num") err.message = `${this.t['Digits only']}`;
            if (err.context.name=="latin") err.message = `${this.t['Entered invalid chars']}`;
            if (err.context.name=="username") err.message = `${this.t['Letters and digits only in english']}`;
          }
    
          return err;
        });
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 2018-07-10
      • 2014-07-07
      • 2020-02-19
      • 2015-12-01
      相关资源
      最近更新 更多