Angular 进阶之( 二 )

{// use #1this.dataResource.getWeather().then(weather => {this.weather = weather;});// use #2this.weather = await this.dataResource.getWeather();// use #3this.weather = await this.dataResource.getWeatherObservable().toPromise();// use #4this.dataResource.getWeatherObservable().subscribe((res: any) => {this.weather = res;},(err: any) => {console.error(err);});// use #5this.weather = await this.dataResource.getWeatherByParams(null, null, { citykey: 101010100 });console.log('this.weather', this.weather);}}
四、运行结果
五、post请求
1. 创建 class具体路径以实际情况为准
import { Injectable } from '@angular/core';import { Resource,ResourceParams,ResourceAction,ResourceRequestMethod,ResourceHandler,IResourceMethodPromise} from '@ngx-resource/core';import { IReturnData } from './auth';@Injectable()@ResourceParams({pathPrefix: '/api'})export class MyAuthResource extends Resource {// will make an post request to /api/login@ResourceAction({method: ResourceRequestMethod.Post,path: '/login'})login: IResourceMethodPromise<{login: string, password: string}, IReturnData>;constructor(restHandler: ResourceHandler ) {super(restHandler);}}
2.
export interface IReturnData {code?: number;message?: number;}
3.使用
// use #6await this.authResource.login({login: 'test', password: 'test'});
Demo
1: 执行npm i
2: 执行 ng serve --open