简体   繁体   English

调用角度解析时属性“ then”丢失'错误

[英]Property 'then' is missing' Error when calling angular resolve

I am using Angular resolver to get data before template get load. 我正在使用Angular解析器在模板加载之前获取数据。 But I am getting an error. 但是我遇到了错误。 Here is my resolver code: 这是我的解析器代码:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';

import { Router, Resolve, RouterStateSnapshot,ActivatedRouteSnapshot } from '@angular/router';

import { ApiService } from './api.service';

@Injectable()
export class ServiceResolve implements Resolve<any> {
  constructor(private apiservices: ApiService, private router: Router) {}

  resolve(route: ActivatedRouteSnapshot): Observable<any> {
    let JsonVal = JSON.parse(localStorage.getItem('RegistrationSteps'));
    let postData = {
      'token': JsonVal.auth_token
    }
    let url = 'getUserDetails';
    return Observable.fromPromise(this.apiservices.getUserDetailsByToken(postData,url));

  }
}

Here is my service code: 这是我的服务代码:

 getUserDetailsByToken(data,url){
    let headers = new Headers();
    return this.http.post(this.base_url+url,data).map(ret=>ret.json());
  }

Here is how I am trying to get data in component: 这是我试图在组件中获取数据的方式:

let userData = this.route.snapshot.data['userdata'];
console.log(userData,'sssss');

here is how my route look like: 这是我的路线:

{path:'signup',component:VendorRegistrationComponent,resolve:{userdata:ServiceResolve}},

This is the error I am getting: 这是我得到的错误:

error TS2345: Argument of type 'Observable' is not assignable to parameter of type 'PromiseLike'. 错误TS2345:无法将类型“可观察”的参数分配给类型“ PromiseLike”的参数。 Property 'then' is missing in type 'Observable'. 类型“可观察”中缺少属性“然后”。

It happen because this 发生是因为

return this.http.post(this.base_url+url,data).map(ret=>ret.json());

Will return Observable, but you are trying to read as Promise Observable.fromPromise(...) , so, just change it to return this.apiservices.getUserDetailsByToken(postData,url); 将返回Observable,但是您尝试将其读为Promise Observable.fromPromise(...) ,因此,只需将其更改为return this.apiservices.getUserDetailsByToken(postData,url); and everything will be ok. 一切都会好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用模态时,从另一个组件调用属性时,Angular 6给出错误“ TypeError:无法读取属性” - Angular 6 gives error“TypeError: Cannot read property” when calling a property from another component while using modal Angular 4错误:类型&#39;()=&gt; any&#39;中缺少属性&#39;includes&#39; - Angular 4 Error : Property 'includes' is missing in type '() => any' 错字错误:角度5中的类型缺少属性数据 - Typo Error:property data missing in type in angular 5 在 Angular 2 中获取“类型错误中缺少属性 X” - Getting "Property X is missing in type error" in Angular 2 错误消息:无法绑定到 xxx,因为在调用 angular 组件时它不是 yyy 的已知属性 - Error message : can't bind to xxx since it isn't a known property of yyy when calling angular component 使用WebPack构建Angular 2时RxJS解决错误 - RxJS Resolve Error When Building Angular 2 with WebPack 在Angular 4中对http请求使用resolve时出错 - Error when using resolve with http requests in Angular 4 如何解决,角度:错误TypeError:无法读取未定义的属性“名称” - How to resolve, Angular: ERROR TypeError: Cannot read property 'name' of undefined 如何解决 angular 中的“类型对象中不存在属性”错误? - How to resolve "property doesn't exists in type object" error in angular? Angular-在构造函数中设置属性时无法解析所有参数 - Angular - Can't resolve all parameters when setting property in constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM