Angular 服务初学者/需要帮助

camsedfj  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(436)

我在使用服务而不是复制代码方面遇到了问题。我如何将其放入服务并在组件中使用?任何帮助都将不胜感激。

public navigateLink(myRecords) {
this.myLink = this.myData.myLinks.myLink;
this.myData.access?
this.openMyModal() :
(myRecords.eligibility?
    this.myService.myStatus(myRecords, this.myData, this.csrfToken) :
    this.myModal2.openModal()
);
this.window.getWindow().location.href = this.myLink;

}

kd3sttzy

kd3sttzy1#

使用angular cli生成服务。跑

ng generate service path/to/your/service/from/src/cool-name

这将创建一个名为coolnameservice的服务
在这里,它默认为root中提供的,这可能是您想要的,因为这段代码看起来可能是一个单例。创建一个包含此逻辑的方法。每个具有“this.”的值都需要作为参数传入,或者作为服务类顶部的服务属性删除。
将此服务传递到组件构造函数中,并使用依赖项注入在组件中即时启动服务。

constructor(private service: CoolNameService) { }

相关问题