从Angular2实例化javascript模块导致错误

时间:2016-06-06 06:16:18

标签: javascript angularjs angular

我有一个角度服务,我想从中使用普通的javascript模块。

当我从构造函数中实例化它时,我得到以下错误:

TypeError: Engine is not a constructor

我的服务如下:

import { Injectable } from "angular2/core";
import * as Engine from './engine';

@Injectable()
export class MyService {

  constructor() {
      this.engine = new Engine(); // causing error
  }

  doStuff() {
    return this.engine.doStuff();
  }

}

我的模块代码:

var _ = require('lodash'); 

function Engine() { 
    this.stuff = [];
};

WorkoutEngine.prototype.doStuff = function() { 
    console.log('do stuff');
};

module.exports = Engine;

我无法弄清楚我哪里出错了?

1 个答案:

答案 0 :(得分:0)

因为您正在尝试将Engine注入MyService的构造函数,

@Injectable()
export class MyService {

  // Let angular inject the engine.   
  constructor(private engine: Engine) {

  }

 .....    
}