在Angular 4中调用服务内部函数,可以按照以下步骤进行操作:
myService.ts
的服务文件:import { Injectable } from '@angular/core';
@Injectable()
export class MyService {
constructor() { }
// 定义需要调用的函数
myFunction(): void {
console.log('Hello from MyService!');
}
}
myComponent.ts
的组件中:import { Component } from '@angular/core';
import { MyService } from './myService';
@Component({
selector: 'my-component',
template: `...`
})
export class MyComponent {
constructor(private myService: MyService) { }
// 在组件中调用服务函数
callServiceFunction(): void {
this.myService.myFunction();
}
}
myComponent.html
模板中:
这样,在点击按钮时,callServiceFunction()
函数将在组件中调用服务函数myFunction()
,并在控制台输出"Hello from MyService!"。
以上是在Angular 4中调用服务内部函数的步骤,希望对你有所帮助!