我有一個角度4應用程序,我試圖寫一個行動的隊列。上一個功能完成後,每個動作都將被觸發並獲取其參數。Angular - 如何使用帶參數的箭頭函數?
public activeRegistrationAndSync() {
let actionsToPerformByOrder = [
() => this.registrationComponent.activeFormValidators.apply(null, arguments),
() => this.registrationComponent.register.apply(null, arguments),
() => this.autoLogin.apply(null, arguments),
() => this.postLogin.apply(null, arguments),
() => this.checkout.apply(null, arguments)
];
let defer = new Deferred<void>();
let curPromise = defer.promise;
//Build chain of acitons
actionsToPerformByOrder.forEach(action => {
curPromise = curPromise
.then(action)
.catch(err => this.error.emit(err));
})
//Active actions
defer.resolve();
}
export class Deferred<T> {
promise: Promise<T>;
resolve: (value?: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
constructor() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
我的問題是箭頭功能,不支持參數和使用功能(){}而不是改變此引用。
「不支持參數」? '(arg1,arg2)=> this.doSomethingWith(arg1,arg2)' – Arg0n
。''相反,你可以在之前定義'let that = this;',而在'functions'中使用'that'。 –
如果您確實需要使用'function(){..'(參數)=> {...}' – Arg0n