亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

非角度類如何使用角度服務及其部門

非角度類如何使用角度服務及其部門

SMILET 2023-04-20 17:14:41
我正在嘗試鏈接 3 個文件 Angular 組件、Plain JS 類和 Angular 服務:我的角度組件(AppComponent)應該初始化我的普通 js 類(CommonCode)...但是,我的普通 js 類需要調用和角度服務。成分:@Component({  selector: 'my-app',  templateUrl: './app.component.html',  styleUrls: [ './app.component.css' ]})export class AppComponent  {  ngOnInit() {  }}服務:import { AnotherAngularService } from '/core/service2';import { AnotherAnotherAngularService } from '/core/service3';@Injectable({  providedIn: 'root',})export class Foo2Service {    this.serviceOneCache = null;    // Service code    constructor(public anotherAngularService:AnotherAngularService, public anotherAnotherAngularService:AnotherAnotherAngularService) {        this.anotherAngularService.getCache().subscribe(response) {                this.serviceOneCache = response;            }            .error();            }    init() {      // Code that returns something      return 'test';    }    getFooCache() {        return this.http.get...... etc etc    }}JS類import { FooService } from '/core/fooservice';export class CommonCode  {    this.fooCache = null;    // Service code    constructor(public fooService: FooService) {        this.fooService.getFooCache().subscribe(response) {                this.fooCache = response;            }            .error();            }    func1() {    }}
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

您可以將CommonCodeTS 類轉換為 Angular 服務并在組件級別提供它。


@Component({

? selector: 'my-app',

? templateUrl: './app.component.html',

? styleUrls: [ './app.component.css' ],

? providers: [ CommonCode ],

})

export class AppComponent? {


? ngOnInit() {


? }

}


查看完整回答
反對 回復 2023-04-20
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號