1.创建一个服务
1.1引入import { Injectable } from '@angular/core';
@Injectable()
export class AppService {
constructor() { }
定义一个方法
}
2.在需要使用的组件的ts文件里引入该服务
2.1 在ngComponent修饰器的providers里加入:providers: [AppService]
2.2 然后初始化:
constructor(private returnPromise: AppService, public alertController: AlertController) { }
2.3使用:
getPromise:any;
confirm() {
this.getPromise = this.returnPromise.giveService();
this.getPromise.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
}
2.4 在页面中用时间属性的方法绑定confirm()方法:
<ion-button color="secondary" (click)="confirm()">点击按钮</ion-button>
以上就是服务的以来注入
依赖注入
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- sefd 在很多清宫剧中都可以看到,宫里的女子都会在脖子上围着一条白巾。 甚至还带有花纹图案 但是在《延禧攻略》里...