导入的服务始终为空
Imported service is always null
我的组件中导入了以下服务,但不知何故它始终为空。
这是我的应用模块:
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TodayComponent } from './components/today/today.component';
import { WeekComponent } from './components/week/week.component';
@NgModule({
declarations: [
AppComponent,
TodayComponent,
WeekComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我的服务
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class TodayService {
constructor(private http: HttpClient) { }
public today(object?): Observable<any> {
return this.http.get(`https://api.openweathermap.org/data/2.5/weather`, { params: object });
}
}
我使用它的组件:
import { TodayService } from '../../services/today.service';
constructor(
public todayService: TodayService,
@Inject(DOCUMENT) private document: Document) {
}
public SUB_today: Subscription;
public success(pos: any) {
var crd = pos.coords;
this.SUB_today = this.todayService.today({lat: crd.latitude, lon: crd.longitude, appid: '**************'}).subscribe((res)=>{
this.today = res;
this.playingWithBG();
})
}
一切似乎都是正确的,我在编译器中没有收到任何错误,但是每当我 运行 我得到的代码:
ERROR TypeError: Cannot read properties of null (reading 'todayService')
直接链接错误与以下代码部分:this.SUB_today = this.todayService.today
尝试在构造函数中注入一个 todaySErvice 的对象,实际上它是空的,因为你不使用它。
错误实际上说 this
是 null
。所以在这里:this.todayService
,您正在尝试阅读 null.todayService
。
this
怎么可能是 null
?由于您调用函数 success
的方式(如问题评论中所述)
navigator.geolocation.getCurrentPosition(this.success, this.error);
您可以显式绑定 this
:
navigator.geolocation.getCurrentPosition(this.success.bind(this), this.error.bind(this));
或提供一个 lambda :
navigator.geolocation.getCurrentPosition(() => this.success(), () => this.error());
我的组件中导入了以下服务,但不知何故它始终为空。
这是我的应用模块:
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TodayComponent } from './components/today/today.component';
import { WeekComponent } from './components/week/week.component';
@NgModule({
declarations: [
AppComponent,
TodayComponent,
WeekComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我的服务
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class TodayService {
constructor(private http: HttpClient) { }
public today(object?): Observable<any> {
return this.http.get(`https://api.openweathermap.org/data/2.5/weather`, { params: object });
}
}
我使用它的组件:
import { TodayService } from '../../services/today.service';
constructor(
public todayService: TodayService,
@Inject(DOCUMENT) private document: Document) {
}
public SUB_today: Subscription;
public success(pos: any) {
var crd = pos.coords;
this.SUB_today = this.todayService.today({lat: crd.latitude, lon: crd.longitude, appid: '**************'}).subscribe((res)=>{
this.today = res;
this.playingWithBG();
})
}
一切似乎都是正确的,我在编译器中没有收到任何错误,但是每当我 运行 我得到的代码:
ERROR TypeError: Cannot read properties of null (reading 'todayService')
直接链接错误与以下代码部分:this.SUB_today = this.todayService.today
尝试在构造函数中注入一个 todaySErvice 的对象,实际上它是空的,因为你不使用它。
错误实际上说 this
是 null
。所以在这里:this.todayService
,您正在尝试阅读 null.todayService
。
this
怎么可能是 null
?由于您调用函数 success
的方式(如问题评论中所述)
navigator.geolocation.getCurrentPosition(this.success, this.error);
您可以显式绑定 this
:
navigator.geolocation.getCurrentPosition(this.success.bind(this), this.error.bind(this));
或提供一个 lambda :
navigator.geolocation.getCurrentPosition(() => this.success(), () => this.error());