我创建了以下名为baseModel.js的基类
import { Validation } from 'aurelia-validation';
let id;
let version;
let validation;
@inject(Validation)
export class BaseModel {
constructor(validation) {
this.validation = validation;
}
}
我在supplierModel.js
中扩展它import { BaseModel } from "./baseModel";
let apSupplierCode;
let discountRate;
export class Supplier extends BaseModel {
constructor() {
super();
super.validation = validation.on(this)
.ensure('apSupplierCode')
.isNotEmpty()
.hasLengthBetween(3, 6)
.ensure('discountRate')
.isNotEmpty()
.containsOnlyDigits()
.isBetween(10, 45.99);
}
}
我在控制台中收到以下错误
inner error: ReferenceError: validation is not defined
at new Supplier http://localhost:9000/dist/models/supplierModel.js:63:55)
我尝试过使用get / set访问器,超级验证和只是验证,但没有区别。
为什么供应商不能在其父(BaseModel)类中看到变量?
答案 0 :(得分:0)
我不认为依赖注入就是这样,它只会注入你正在创建的当前对象。这可能会破坏目的,但您需要做的是:
import { Validation } from 'aurelia-validation';
export class Supplier extends BaseModel {
@inject(Validation)
constructor(validation) {
super(validation);
// Do stuff
}
}
另一种选择是从基类
中的容器中显式加载依赖项import {Container} from 'aurelia-dependency-injection';
import {Validation} from 'aurelia-validation';
export class BaseModel {
constructor() {
this.validation = Container.instance.get(Validation);
}
}