2016-08-18 25 views
0

我在我的应用程序的几个地方使用EventAggregator,并且一切正常。不过,我无法将其注入我的课程的一个。当调用类构造函数时,EventAggregator参数未定义。无法将Aurelia的EventAggregator注入特定的类

import {EventAggregator} from 'aurelia-event-aggregator'; 
import {inject} from 'aurelia-framework'; 

@inject(EventAggregator) 
export class Test { 
    constructor(eventAggregator) { 
     debugger; 
     this.eventAggregator = eventAggregator; 
    } 
} 

当在debugger;线停止,构造函数的参数eventAggregator是不确定的。

这看起来就像我在其他许多类中使用EventAggregator所做的一样,那么可能会出现什么问题?

+0

你使用jspm吗?如果是这样,'jspm inspect -forks'会返回什么?在你的断点处,EventAggregator的价值是多少? –

+0

@JeremyDanyow它返回:已安装的分叉 npm:aurelia-event-aggregator 1.0.0-beta.1.2.0 1.0.0-beta.1.2.1 npm:aurelia-logging 1.0.0-beta.1.2.0 1.0.0-beta.1.2.1 –

+0

@JeremyDanyow EventAggregator的值为函数EventAggregator(){....},但eventAggregator参数未定义。 –

回答

1

我认为这与叉(安装相同软件包的多个版本)有关。

  1. 要清除它,请打开您的config.js文件并删除map: {节点中的所有内容。例如,所有this
  2. 然后执行jspm install
  3. 然后再次检查叉子。 jspm inspect --forks
  4. 如果您仍然有叉,执行jspm install aurelia-_______each aurelia item listed in your package.json

在实况,你可能想直接跳到第4步 - 这将确保您获得最新,也就是现在的1.0.0(RTM)版本的aurelia。

+1

提前升级到Aurelia 1.0,但这并没有解决问题。事实证明,我在刚刚添加EventAggregator的注入类的类上调用new()。所以当类的构造函数被调用时,eventAggregator参数没有被传递,所以它当然是未定义的。 –

+0

啊,那会做!至少你升级了,叉子被清理了 –

+0

现在的问题是,如何在一个不是单例的类中使用依赖注入,因为消费者在调用new()时无法访问注入的类?使用默认参数? –