不能使用注入的服务

时间:2016-05-30 11:26:25

标签: typescript angular

将简单服务导入login.component.ts:`

import { Component } from '@angular/core';
import {TestService} from '../login/TestService';

@Component({
    providers: [TestService],
    templateUrl: './app/login/loginTemplate.html'
})
export class loginComponent  {
    constructor(testService: TestService){}

    sendLoginRequest() {
        this.testService.writeLine();
    }
}

这是TestService文件

import { Injectable } from '@angular/core';

@Injectable ()
export class TestService {
    constructor () {}

    writeLine () {
         return console.log("works from service");
    }
}

1 个答案:

答案 0 :(得分:2)

对于此简短形式,privatepublic是必需的

constructor(private testService: TestService){}

没有这个访问修饰符你需要做

testService;TestService;
constructor(testService: TestService){
  this.testService = testService;
}