鍍金池/ 問答/HTML5/ 求一個(gè)可以集中管理接口的服務(wù)《代碼》??。?!

求一個(gè)可以集中管理接口的服務(wù)《代碼》??。?!

1,最開始我先把接口寫在每個(gè)component.ts里面

clipboard.png

2,現(xiàn)在我需要把請求接口的方法集中放在server.ts里面,求一個(gè)思路代碼。謝謝大佬

clipboard.png

回答
編輯回答
離人歸

說一下我的做法,不知道符不符合你的要求
我是把接口url寫到一個(gè)類里面:

export class ServicePath {
    public static SHOPLIST = 'xxx.xxx';
}

服務(wù)里面只是進(jìn)行http請求,跟官網(wǎng)的案例寫法一樣:

getList(data) {
    return this.http.post(ServicePath.SHOPLIST, data)
}

然后在組件里面引入服務(wù)請求處理數(shù)據(jù):

this.service.getList(data).subscribe(res => {
    console.log(res);
})
2018年9月8日 22:16