将 Ionic 3 迁移到 Ionic 5 API
Migrating Ionic 3 to Ionic 5 API
你好,我有一个 Ionic 3 项目,我想迁移到 Ionic 5。我知道这些组件,但我遇到了不再支持的 Http 问题。
我曾经像这样调用 .ts 函数从 API 调用数据
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { AppData } from '../../providers/app-data/app-data';
@Injectable()
export class UserData {
options: any;
api_signature: string = '';
constructor(
public http: Http,
public device: Device,
public events: Events
) {
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
this.options = new RequestOptions({
headers: headers
});
}
并充当
allUsers(offset: number) {
let url = this.appData.getApiUrl() + 'allUsers';
let data = this.jsonToURLEncoded({
offset:offset
});
return this.http.post(url, data, this.options);
}
现在我像这样将 http 称为 HttpClient。但是我遇到了问题 RequestOptions is redlighted ana says can't find module which I know is not longer supported
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { AppData } from '../app-data/app-data';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UserData {
options: any;
constructor(
public storage: Storage,
public appData: AppData,
public http: HttpClient,
) {
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
this.options = new RequestOptions({
headers: headers
});
}
和这样的函数
allUsers(offset: number) {
const url = this.appData.getApiUrl() + 'allUsers';
const data = this.jsonToURLEncoded({
offset: offset
});
return this.http.post(url, data, this.options);
}
我也有 rxjs 和 map 的问题。这就是我在 .ts 中调用函数以从 api
获取数据的方式
allUsersSet() {
this.userData.allUsers(this.offset)
.map(res => res.json())
.subscribe(data => {
if (data.success) {
const temp = data.usersFeed;
this.allUsers = temp;
}
});
}
.map 被红线标记并表示在我放置的 Observable 上不存在
import { map } from 'rxjs/operators';
但是 .map 仍然出错
我做到了
allUsersSet() {
this.userData.allUsers(this.offset)
map((data: any) => {
if (data.success) {
const temp = data.usersFeed;
this.allUsers = temp;
}
});
}
我没有收到错误,但 api 调用未显示在 Inspect/Network/XHR 选项卡中
有什么帮助吗?
尝试使用 HttpHeaders 而不是 RequestOptions
import { HttpHeaders } from '@angular/common/http';
this.options = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
});
然后像这样使用它:
return this.http.post(url, data, this.options);
您可以在此处查看有关 api 较新版本的文档:https://angular.io/guide/http
你的地图问题,需要使用pipe
this.userData.allUsers(this.offset).pipe(
map((data: any) => {
if (data.success) {
const temp = data.usersFeed;
this.allUsers = temp;
}
})
);
此处可观察文档:https://rxjs-dev.firebaseapp.com/guide/operators
除非您调用 .subscribe()
,否则不会调用 API
this.userData.allUsers(this.offset).pipe(map()).subscribe()
你也可以用这个。
import { HttpClient, HttpParams } from '@angular/common/http';
allUsers(offset: number) {
const url = this.appData.getApiUrl() + 'allUsers';
const requestOptions = {
params: new HttpParams()
};
const data = this.jsonToURLEncoded({
api_signature: this.api_signature,
offset: offset
});
return this.http.post(url, data, requestOptions );
}
你好,我有一个 Ionic 3 项目,我想迁移到 Ionic 5。我知道这些组件,但我遇到了不再支持的 Http 问题。
我曾经像这样调用 .ts 函数从 API 调用数据
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { AppData } from '../../providers/app-data/app-data';
@Injectable()
export class UserData {
options: any;
api_signature: string = '';
constructor(
public http: Http,
public device: Device,
public events: Events
) {
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
this.options = new RequestOptions({
headers: headers
});
}
并充当
allUsers(offset: number) {
let url = this.appData.getApiUrl() + 'allUsers';
let data = this.jsonToURLEncoded({
offset:offset
});
return this.http.post(url, data, this.options);
}
现在我像这样将 http 称为 HttpClient。但是我遇到了问题 RequestOptions is redlighted ana says can't find module which I know is not longer supported
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { AppData } from '../app-data/app-data';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UserData {
options: any;
constructor(
public storage: Storage,
public appData: AppData,
public http: HttpClient,
) {
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
this.options = new RequestOptions({
headers: headers
});
}
和这样的函数
allUsers(offset: number) {
const url = this.appData.getApiUrl() + 'allUsers';
const data = this.jsonToURLEncoded({
offset: offset
});
return this.http.post(url, data, this.options);
}
我也有 rxjs 和 map 的问题。这就是我在 .ts 中调用函数以从 api
获取数据的方式allUsersSet() {
this.userData.allUsers(this.offset)
.map(res => res.json())
.subscribe(data => {
if (data.success) {
const temp = data.usersFeed;
this.allUsers = temp;
}
});
}
.map 被红线标记并表示在我放置的 Observable 上不存在
import { map } from 'rxjs/operators';
但是 .map 仍然出错
我做到了
allUsersSet() {
this.userData.allUsers(this.offset)
map((data: any) => {
if (data.success) {
const temp = data.usersFeed;
this.allUsers = temp;
}
});
}
我没有收到错误,但 api 调用未显示在 Inspect/Network/XHR 选项卡中
有什么帮助吗?
尝试使用 HttpHeaders 而不是 RequestOptions
import { HttpHeaders } from '@angular/common/http';
this.options = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
});
然后像这样使用它:
return this.http.post(url, data, this.options);
您可以在此处查看有关 api 较新版本的文档:https://angular.io/guide/http
你的地图问题,需要使用pipe
this.userData.allUsers(this.offset).pipe(
map((data: any) => {
if (data.success) {
const temp = data.usersFeed;
this.allUsers = temp;
}
})
);
此处可观察文档:https://rxjs-dev.firebaseapp.com/guide/operators
除非您调用 .subscribe()
,否则不会调用 APIthis.userData.allUsers(this.offset).pipe(map()).subscribe()
你也可以用这个。
import { HttpClient, HttpParams } from '@angular/common/http';
allUsers(offset: number) {
const url = this.appData.getApiUrl() + 'allUsers';
const requestOptions = {
params: new HttpParams()
};
const data = this.jsonToURLEncoded({
api_signature: this.api_signature,
offset: offset
});
return this.http.post(url, data, requestOptions );
}