在类对象上使用验证而不在Aurelia中进行注入 [英] Use Validation on class object without injection in Aurelia
问题描述
我正面临与Aurelia注射相关的一个问题. 我想知道如何在没有注入的情况下实现 Validation,EventAggregator和Router .
I am facing one issue related to the injection(s) in Aurelia. I was wondering how to implement Validation, EventAggregator and Router without injection.
下面您可以找到一个示例,该示例可以使您清楚地了解实施情况以及遇到的困难.
Below you can find an example which may give you a clear picture about the implementation and where I am stuck.
类配置文件与视图进行交互,并在 profile类中创建 AddressList 的对象,并且此对象(AddressList)与视图进行交互.视图.
class Profile interacts with the view, and object of AddressList is created in the profile class and this object(AddressList) interacts with the view.
例如:
@inject(EventAggregator, Validation, Router)
export class Profile{
addressList: Array<AddressList> = [];
eventAgg:any;
_validation:any;
_router:any;
constructor(EventAggregator, Validation, Router )
{
this.eventAgg = EventAggregator;
this._validation = Validation;
this._router = Router;
this.addressList.push(new AddressList());
}
}
export class AddressList{
street1:string = "street1";
street2:string = "street2";
constructor(){
}
现在,我想对AddressList属性进行验证,而无需在AddressList的构造方法中传递Validation
Now I want to implement validations on the properties of AddressList without passing Validation in the construtor of AddressList
我不要这个
this.addressList.push(new AddressList(Valdiation));
因为当我想在AddressList的构造函数中传递参数时,这会产生问题.
Because this will create issues when I want to pass arguments in the constructor of the AddressList.
我认为当我们尝试在另一个视图模型中组合一个视图模型并且构造函数期望一些用户定义的参数时,也会发生此问题.
I think this issue will also occur when we will try to compose one view-model in the other view-model and constructor expects some user defined arguments.
预先感谢
安库尔
问题的更新/更改
我按照 Matthew James Davis 的建议进行了更改. 但是我不明白为什么AddressList会变得未定义.
I done the changes as suggested by Matthew James Davis. But I am unable to understand why AddressList is coming as undefined.
更新代码
import { Factory } from 'aurelia-framework';
import { ObserverLocator } from 'aurelia-framework';
import { EventAggregator } from 'aurelia-event-aggregator';
import { Validation, ensure } from 'aurelia-validation';
@inject(EventAggregator, Validation, Factory.of(AddressList))
export class Profile{
addressList: Array<AddressList> = [];
eventAgg:any;
_validation:any;
_router:any;
constructor(EventAggregator, Validation, AddressList)
{
this.eventAgg = EventAggregator;
this._validation = Validation;
this.addressList.push(AddressList(["street1","street2"]));
}
}
@inject(Validation)
export class AddressList{
street1:string = "street1";
street2:string = "street2";
constructor(Validation, args){
this.street1=args[0];
this.street2=args[1];
}
}
控制台错误
AddressList
function() {
for (var _len = arguments.length, rest = Array(_len), _key = 0; _key < _len; _key++) {
rest[_key] = arguments[_key];
}
return container.invoke(_this2._…
AddressList ()
错误是由于Container.prototype._createInvocationHandler中的这一行引起的:
Error is due to this line in Container.prototype._createInvocationHandler:
if (fn.inject === undefined)
fn未定义.
我认为这可能会对您有所帮助,但我仍在尝试找出可能是问题所在.
I think it might help you, and I am still trying to figure out what can be the issue.
推荐答案
我不确定这是否是问题,但是当我使用打字稿时,我知道您还需要从aurelia-框架.
I'm not sure if this is the problem, but as I'm using typescript, I know that you also need to import inject
from aurelia-framework.
另一种可能性是像这样注入它:
Another possibility would be to inject it like this:
private static inject = [EventAggregator, Validation, Factory.of(AddressList)]
但是,如果您不使用打字稿,则不确定是否有必要.
But if you're not using typescript, I'm not sure if this is necessary.
这篇关于在类对象上使用验证而不在Aurelia中进行注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!