将简单服务导入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");
}
}
答案 0 :(得分:2)
对于此简短形式,private
或public
是必需的
constructor(private testService: TestService){}
没有这个访问修饰符你需要做
testService;TestService;
constructor(testService: TestService){
this.testService = testService;
}