簡體   English   中英

Aurelia:@autoinject不會注入Element

[英]Aurelia: @autoinject does not inject Element

我的自定義屬性中有@autoinject()裝飾器,我有一些奇怪的bevaviour。

該屬性的構造函數如下所示:

constructor(element: Element,translationService: TranslationService, eventAggregator: EventAggregator){
    console.log(element);
    console.log(translationService);
    console.log(eventAggregator);
    ...
}

該類使用@autionject()進行@autionject() ,而正確注入參數translationServiceeventAggregatorelement參數將填充一個絕對不是元素的對象。

元素看起來像這樣:

{
    jQuery321051167339178565241 : {events: {…}, handle: ƒ}
    __proto__: Object
}

當我使用@inject(Element, TranslationService, EventAggregator)而不是@autoinject() ,元素被正確注入。

有人猜猜出了什么問題嗎?

你得到的是一個jquery包裝元素(例如,如果你調用$(el)你得到的),所以TypeScript可能會以某種方式發出錯誤的類型元數據。

為了能夠解決這個問題,您需要包括您的打字稿版本,tsconfig,構建配置和Aurelia版本。

在此期間,當您使用@autoinject()並搜索與您的自定義屬性綁定的“design:paramTypes”時,請查看應用程序的發出.js 它應該看起來像這樣:

exports.MyCustomAttribute = __decorate([
    aureliaDependencyInjection.autoinject(),
    __metadata("design:paramtypes", [Element])
], exports.MyCustomAttribute);

然后切換到@inject(Element) ,構建並執行相同的操作。 你應該找到這樣的東西:

exports.MyCustomAttribute = __decorate([
    aureliaDependencyInjection.inject(Element),
    __metadata("design:paramtypes", [Element])
], exports.MyCustomAttribute);

查看傳遞給aureliaDependencyInjection.inject(..)Element對象是否與傳遞給aureliaDependencyInjection.inject(..)Element對象不同__metadata("design:paramtypes", ..)

這應該有助於排除TypeScript是否確實發出錯誤的元數據,或者是否有其他錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM