我正在尝试从数据库中获取用户。当传递用户名作为参数时,控制器服务应返回控制器。 控制器只是一种特定的用户。
当我尝试将已解析的用户解析为Controller时遇到的错误是
Argument of type 'Observable<Controller>' is not assignable to parameter of type 'string'
这是发生错误的部分
this.currentController = JSON.parse(controllerService.getController(token.getUsername()));
下面是我的代码。
控制器服务
import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Controller } from '../models/controller.model';
import {catchError} from 'rxjs/operators';
import { throwError} from 'rxjs';
import { FilterSearchControllerDTO } from
'../models.dto/filterSearchController.dto';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ControllerService{
constructor(private http:HttpClient) {}
private controllerUrl = 'http://localhost:8080/api/controller/';
public getControllers() {
return this.http.get<Controller[]>(this.controllerUrl);
}
public getController(username: string) {
return this.http.get<Controller>(`${this.controllerUrl + 'getController'}/${username}`);
}
public deleteController(id: BigInteger){
const url = `${this.controllerUrl + 'deleteController'}/${id}`;
return this.http.get<boolean>(url);
}
public addController(controller: Controller){
return this.http.post(this.controllerUrl + 'addController',controller)
.pipe(
catchError(e => throwError(e))
);
}
public filterSearch(values: FilterSearchControllerDTO){
return this.http.post<Controller[]>(this.controllerUrl + 'searchFilter',values);
}
}
ControllerView组件
import { Component, OnInit, ViewChild } from '@angular/core';
import { HeaderComponent } from '../header/header.component';
import { ControllerService } from '../services/controller.service';
import { Controller } from '../models/controller.model';
import { Router } from '@angular/router';
import { TokenStorageService } from '../auth/token-storage.service';
@Component({
selector: 'app-controller-view',
templateUrl: './controller-view.component.html',
styleUrls: ['./controller-view.component.css']
})
export class ControllerViewComponent implements OnInit {
currentController: Controller;
@ViewChild("header") header: HeaderComponent;
showView: string = 'home';
constructor(private controllerService: ControllerService, private router: Router, private token: TokenStorageService) {
this.currentController = JSON.parse(controllerService.getController(token.getUsername()));
}
ngOnInit() {
this.header.controllerView();
}
onNavigate(feature: string){
this.showView = feature;
if(feature == 'logout') {
window.sessionStorage.clear();
this.router.navigate(['mainPage']);
window.alert("Successfully Logged out!");
}
}
}
答案 0 :(得分:0)
在您的服务getController()
方法中返回一个Observable<Controller>
。因此,您基本上不能从中返回Controller
。相反,您需要订阅它并获得如下值:
ControllerView组件
constructor(private controllerService: ControllerService, private router: Router, private token: TokenStorageService) {
this.controllerService.getController(token.getUsername()).subscribe((response) => {
this.currentController = response;
})
}