大佬教程收集整理的这篇文章主要介绍了angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object]),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经检查了几乎每个主题,并尝试了多种方法来解决它,但仍然无法在第二天击败它.
我试图像这样在appservice中注入第一个authservice但是得到了同样的错误
@Inject(forWARDRef(() => AuthenticationservicE)) public authservice: Authenticationservice
我检查了所有DI和服务内部的导入顺序,在我看来一切都是正确的
如果有人可以帮我处理它,我很感激.
Angular 4.0.0
Authservice
import { Injectable } from '@angular/core'; import {http,Headers,ResponsE} from '@angular/http'; import 'rxjs/add/operator/toPromise'; import {ObservablE} from 'rxjs/Rx'; import {Appservices} from "../../app.services"; import {Router} from "@angular/router"; @Injectable() export class Authenticationservice { public token: any; constructor( private http: http,private appservice: Appservices,private router: Router ) { this.token = localStorage.getItem('token'); } login(username: String,password: String): Observable<Boolean> { let headers = new Headers(); let body = null; headers.append("Authorization",("Basic " + btoa(username + ':' + password))); return this.http.post(this.appservice.api + '/login',body,{headers: headers}) .map((response: ResponsE) => { let token = response.json() && response.json().token; if (token) { this.token = token; localStorage.setItem('Conform_token',token); return true; } else { return false; } }); } logout(): void { this.token = null; localStorage.removeItem('Conform_token'); this.router.navigate(['/login']); } }
应用服务
import {InjectablE} from '@angular/core'; import {Headers,http,requestOptions} from '@angular/http'; import {Router} from "@angular/router"; import {AuthenticationservicE} from "./auth/auth.service"; import 'rxjs/add/operator/toPromise'; import {ObservablE} from 'rxjs/Rx'; @Injectable() export class Appservices { api = '//endpoint/'; public options: any; constructor( private http: http,private router: Router,public authservice: Authenticationservice // doesn't work // @Inject(forWARDRef(() => AuthenticationservicE)) public authservice: Authenticationservice // doesn't work either ) { let head = new Headers({ 'Authorization': 'Bearer ' + this.authservice.token,"Content-Type": "application/json; charset=utf8" }); this.options = new requestOptions({headers: heaD}); } // ==================== // data services // ==================== getData(): Promise<any> { return this.http .get(this.api + "/data",this.options) .toPromise() .then(response => response.json() as Array<Object>) .catch((err)=>{this.handleError(err);}) }
应用模块
import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import {BaserequestOptions,httpR_697_11845@odulE} from '@angular/http'; import { MaterialModulE} from '@angular/material'; import {FlexLayoutModulE} from "@angular/flex-layout"; import 'hAMMerjs'; import { routIng,appRoutIngProviders } from './app.routIng'; import { Appservices } from './app.services'; import {AuthGuarD} from "./auth/auth.guard"; import {AuthenticationservicE} from "./auth/auth.service"; import {AppComponent} from './app.component'; import {AuthComponent} from './auth/auth.component'; import {NotFoundComponent} from './404/not-found.component'; import { HomeComponent } from './home/home.component'; @NgModule({ declarations: [ AppComponent,AuthComponent,NotFoundComponent,HomeComponent ],imports: [ BrowserModule,BrowserAnimationsModule,FormsModule,httpR_697_11845@odule,routIng,MaterialModule,FlexLayoutModule ],providers: [Appservices,AuthGuard,Authenticationservice],bootstrap: [AppComponent] }) export class AppModule { }
你可以使用
export class Authenticationservice { public token: any; appservice: Appservices; constructor( private http: http,// private appservice: Appservices,injector:Injector; private router: Router ) { setTimeout(() => this.appservice = injector.get(Appservices)); this.token = localStorage.getItem('token'); }
另见DI with cyclic dependency with custom HTTP and ConfigService
要避免使用setTimeout,您还可以从Appservice的构造函数中设置Authenticationservice.appservice(或者相反)
以上是大佬教程为你收集整理的angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])全部内容,希望文章能够帮你解决angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。