注入服务 returns 异常
Injecting service returns exception
我正在尝试向组件注入服务,但收到以下消息
EXCEPTION: TypeError: Cannot read property 'getDemoString' of undefined
据我所知,服务没有被注入。
组件
import {Component, OnInit} from 'angular2/core'
import {DemoService} from './demo.service'
@Component({
selector: 'gtbe-navbar',
templateUrl: 'app/navbar.component.html',
providers: [DemoService]
})
export class Navbar implements OnInit {
name: string;
constructor(private _service: DemoService) { }
ngOnInit() {
this.name = this._service.getDemoString();
}
}
服务
import {Injectable} from 'angular2/core'
@Injectable()
export class DemoService {
getDemoString() {
return "demo";
}
}
我成功地将我的服务注入到组件中。我将 angular 版本更新到 RC,它可以工作。
我在使用 NPM 更新它时遇到了一些问题,因为 npm 和节点版本,我也更新了它们并且它有效。
感谢@Alfons Ingomar。
我正在尝试向组件注入服务,但收到以下消息
EXCEPTION: TypeError: Cannot read property 'getDemoString' of undefined
据我所知,服务没有被注入。
组件
import {Component, OnInit} from 'angular2/core'
import {DemoService} from './demo.service'
@Component({
selector: 'gtbe-navbar',
templateUrl: 'app/navbar.component.html',
providers: [DemoService]
})
export class Navbar implements OnInit {
name: string;
constructor(private _service: DemoService) { }
ngOnInit() {
this.name = this._service.getDemoString();
}
}
服务
import {Injectable} from 'angular2/core'
@Injectable()
export class DemoService {
getDemoString() {
return "demo";
}
}
我成功地将我的服务注入到组件中。我将 angular 版本更新到 RC,它可以工作。 我在使用 NPM 更新它时遇到了一些问题,因为 npm 和节点版本,我也更新了它们并且它有效。 感谢@Alfons Ingomar。