所以我試圖對我的 API 進行快速測驗,但我遇到了這個奇怪的問題......
這是我的 ApiService 代碼:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class ApiService {
constructor(private http: HttpClient) { }
async getData(url, method): Promise<Object>{
let fullurl = url method
console.log(fullurl);
return await new Promise(resolve=>{
this.http.get(fullurl)
.subscribe(data=>{
resolve(data);
return data;
},error=>{
console.log(error);
});
});
}
}
這就是我如何稱呼我的服務:
import { Component, OnInit } from '@angular/core';
import { ApiService } from '../services/api.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
providers:[ApiService]
})
export class HomePage implements OnInit {
constructor( private api:ApiService,) {}
url:string="api.test.tk/"
method:string="SangreDragon"
json:any
async ngOnInit(){
console.log(this.url this.method);
this.json = await this.api.getData(this.url, this.method);
await console.log(this.json);
}
}
GET 呼叫以某種方式轉到:http://localhost:8100/api.test.tk/SangreDragon
我使用 Ionic 服務來測驗它......我應該編譯來測驗它嗎?
為什么?
uj5u.com熱心網友回復:
很明顯...如果您不在URL 上輸入http://,它會嘗試轉到 localhost...所以它是固定的。謝謝你的閱讀,我猜。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424038.html
標籤:有角度的 打字稿 离子框架 httpclient
