Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了角度错误:[object Object] resolvePromise大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Angular 7并使用observable调用简单服务

import { Injectable } from '@angular/core';
import { http,Headers} from '@angular/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class Authservice {
  constructor(private http:http) { }
  registerUser(user):Observable<any>{
    let headers = new Headers();
    headers.append('Content-Type','application/json');
    return this.http.post('http://localhost:8888/users/register',user,{headers:headers});
  }
}

然编译代码是好的,但当它命中注册url和组件然后它给出错误

ERROR Error: "[object Object]"
    resolvePromise http://localhost:4200/polyfills.js:3159:31
    resolvePromise http://localhost:4200/polyfills.js:3116:17
    scheduleResolveOrReject http://localhost:4200/polyfills.js:3218:17
    invokeTask http://localhost:4200/polyfills.js:2766:17
    onInvokeTask http://localhost:4200/vendor.js:72221:24
    invokeTask http://localhost:4200/polyfills.js:2765:17
    runTask http://localhost:4200/polyfills.js:2533:28
    drainMicroTaskQueue http://localhost:4200/polyfills.js:2940:25
vendor.js:70671:5
defaultErrorLogger
http://localhost:4200/vendor.js:70671:5
./node_modules/@angular/core/fesm5/core.js/ErrorHandler.prototype.handleError
http://localhost:4200/vendor.js:70719:9
next
http://localhost:4200/vendor.js:72698:111
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.subscribe/schedulerFn<
http://localhost:4200/vendor.js:68245:36
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.__tryOrUnsub
http://localhost:4200/vendor.js:141501:13
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.next
http://localhost:4200/vendor.js:141439:17
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype._next
http://localhost:4200/vendor.js:141382:9
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype.next
http://localhost:4200/vendor.js:141359:13
./node_modules/rxjs/_esm5/internal/Subject.js/Subject.prototype.next
http://localhost:4200/vendor.js:141124:25
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.emit
http://localhost:4200/vendor.js:68229:54
onHandleError/<
http://localhost:4200/vendor.js:72252:57
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke
zone.js:388
./node_modules/zone.js/dist/zone.js/</Zone.prototype.run
zone.js:138
./node_modules/@angular/core/fesm5/core.js/NgZone.prototype.runOutsideAngular
http://localhost:4200/vendor.js:72189:16
onHandleError
http://localhost:4200/vendor.js:72252:13
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.handleError
zone.js:392
./node_modules/zone.js/dist/zone.js/</Zone.prototype.runGuarded
zone.js:154
_loop_1
zone.js:677
./node_modules/zone.js/dist/zone.js/</</api.microtaskDrainDone
zone.js:686
drainMicroTaskQueu
e
zone.js:602

如果我从’../../services/auth.service’中删除组件// import {AuthservicE}中的服务类;然后代码运行没有错误.
尝试返回简单的返回用户,但仍然出现错误.
也尝试了没有观察到的回归.
你能帮助我做错的地方吗?

解决方法

自Angular 5以来,http的使用已被弃用.因此它不会在Angular 7中提供.

你应该使用httpClient而不是http.此外,认情况下会添加Content-Type标头.所以你不需要明确地添加它.

最后,http.post已经返回一个Observable.所以你不需要把它包装成一个

import { Injectable } from '@angular/core';
import { httpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class Authservice {

  constructor(private http: httpClient) {}

  registerUser(user): Observable <any> {
    return this.http.post('http://localhost:8888/users/register',user);
  }

}

还必须将httpClientModule添加到AppModule的imports数组中:

import { httpClientModule } from '@angular/common/http';

@NgModule({
  ...,imports: [...,httpClientModule,...]
  ...
})

然后在你的组件中:

constructor(private authservice: AuthservicE) {}
...
const user = ...

this.authservice.registerUser(user)
  .subscribe(response  => console.log(responsE));
...

大佬总结

以上是大佬教程为你收集整理的角度错误:[object Object] resolvePromise全部内容,希望文章能够帮你解决角度错误:[object Object] resolvePromise所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。