angular - LỖI TypeError: Không thể đọc thuộc tính 'handle' của undefined
Trong ứng dụng của tôi, tôi đã tạo hai thành phần. Thành phần Selector và thành phần Shifter. Thành phần Selector chọn danh sách các mục cần được cập nhật. Thành phần selector gọi một phương thức trong thành phần Shifter để thực sự cập nhật mục đã chọn. Thành phần selector gọi một dịch vụ, dịch vụ này lần lượt gọi một URL sử dụng phương thức HTTP Get để lấy danh sách các mục cần cập nhật. Tôi liệt kê các mục đã chọn dưới dạng các liên kết. Khi nhấp vào một mục được liệt kê, một cuộc gọi đến phương thức trong thành phần Shifter sẽ được thực hiện. Phương thức này gọi một dịch vụ, dịch vụ này lần lượt gọi phương thức HTTP Patch để cập nhật mục.
Trong quá trình này tôi đang gặp lỗi
core.js:6014 LỖI TypeError: Không thể đọc thuộc tính 'handle' của undefined
Đây là thành phần bộ chọn của tôi:
import { Component, OnInit } from '@angular/core';
import { SelectorService } from '../selector.service';
import { Item, SelectorResponse } from '../selector-response';
import { ShifterRequest } from '../shifter-request';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {CanonicalShifterRequest } from '../cshifter';
import { ShifterService } from '../shifter.service';
import { ShifterComponent } from '../shifter/shifter.component';
@component({
selector: 'app-selector',
template:
<div *ngIf="sdata">
<h2>Items</h2>
<ul>
<li *ngFor="let tci of getTags()">
<a href="javascript:void90);" (click)="callupdate(tci.slector, tci.service)">{{tci.service}] - {{tci.selector}}</a>
</li>
</ul>
</div>
,
styles: []
})
export class SelectorComponent implements OnInit {
constructor(private _selectorService: SelectorService){}
public svs:ShifterComponent;
public sdata: SelectorResponse;
public cShifterRequest: CanaonicalShifterRequest[] =[];
public services:string[] = ["myservice1",....
public httpH:HttpHandler;
public httpC:HttpClient = new HttpClient(this.httpH);
ngOnInit(){
this._selectorService.getEntries().subscribe(data => this.sdata = data);
}
getTags(){
//my method to get the items
}
callUpdate(instanceSel:String, servicename: String){
var shifterComponent:ShifterComponent(new ShifterService(this.httpc));
if (shifterComponent instanceof ShifterComponent)
shifterComponent.updateServiceCaller(instanceSel, servicename);
}
}
Đây là bộ phận cần số của tôi:
import { Component, OnInit } from '@angular/core';
import { SelectorService } from '../selector.service';
import { Item, SelectorResponse } from '../selector-response';
import { ShifterRequest } from '../shifter-request';
import { HttpClient } from '@angular/common/http';
import {CanonicalShifterRequest } from '../cshifter';
import { ShifterService } from '../shifter.service';
@Component({
selector: 'app-shifter',
template: <h2>update</h2>,
styles: []
})
export class ShifterComponent implements OnInit {
constructor(private _shifterService: ShifterService ) {}
public instanceSel:String:
public servicename:String;
public udata: any;
updateServiceCaller(instanceSel:String, servicename:String){
if(this._shifterService instanceof ShifterService)
this._shifterService.updateService(instanceSel, servicename).subscribe(data => this.udata = data);
}
}
Đây là dịch vụ cần số của tôi
pimport { Observable } from 'rxjs';
import { ShifterRequest } from './shifter-request';
@Injectable({
providedIn: 'root'
})
export class ShifterService{
private servicename:String;
private _url:String = "";
shifterRequest:
headerDict = {... } //my headers
headers = new HttpHeaders(this.headerDict)
constructor(private http: HttpClient){}
updateService(instanceSel: String, servicename: String) : Observable<any> {
this._url="https://xyz/" + servicename;
if(this.http instanceof HttpClient){
return this.http.patch<any>(this._url, {..my request goes here.}, {headers: this.headers});
}
else{
console.log("shifter service nul http");
return null;
}
}
}
Tôi nghĩ vấn đề là do tôi không chắc làm thế nào để khởi tạo HttpClient trong SelectorComponent.
Có ai đó có thể giúp giải quyết vấn đề của tôi không.
Câu trả lời #1
để khởi tạo HttpClient chỉ cần yêu cầu nó trong constructor, giống như bạn đã làm trong SelectorComponent cho SelectorService
export class SelectorComponent implements OnInit {
constructor(private _selectorService: SelectorService,
private httpC: HttpClient){}
Angular sẽ đọc các "params" này và cung cấp tất cả các dịch vụ cần thiết cho bạn thông qua Dependency Injection. Sau khi việc này hoàn tất, nếu bạn gặp lỗi gì đó như Cannot find provider for HttpClient, chỉ cần thêm HttpClientModule vào imports của AppModule của bạn
