首页 > 解决方案 > Angular 在尝试导航应用程序时抛出错误

问题描述

所以我正在用 angular 和 laravel 构建一个应用程序。一切都运行良好——我已经登录并专注于我自己的事情。然后,我注销了,当我想重新登录我的应用程序时,我不再合作了。我在后端使用 laravel 和 JWT。我试图去应用程序中的不同路线,如登录或注册,我收到错误:

 ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[false -> false -> false]: 
  NullInjectorError: No provider for false!
NullInjectorError: R3InjectorError(AppModule)[false -> false -> false]: 
  NullInjectorError: No provider for false!

我的应用模块:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { LoginComponent } from './components/login/login.component';
import { SignupComponent } from './components/signup/signup.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpService } from './http.service';
import { ResultsComponent } from './components/dashboard/results/results.component';
import { PricingComponent } from './components/pricing/pricing.component';

@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    LoginComponent,
    SignupComponent,
    DashboardComponent,
    ResultsComponent,
    PricingComponent,
  ],
  imports: [BrowserModule, AppRoutingModule, FormsModule, HttpClientModule],
  providers: [HttpService],
  bootstrap: [AppComponent],
})
export class AppModule {}

任何帮助,将不胜感激!

编辑:添加 HTTP 服务:

    import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root',
})
export class HttpService {
  constructor(private http: HttpClient) {}
  private baseUrl = 'http://localhost:8000/api';
  login(form) {
    return this.http.post(`${this.baseUrl}/login`, form);
  }
  signup(form) {
    return this.http.post(`${this.baseUrl}/signup`, form);
  }
  findVehicle(vehicleNumber = 'empty') {
    return this.http.get(`${this.baseUrl}/findVehicle/${vehicleNumber}`);
  }
  getPricing(year) {
    return this.http.get(`${this.baseUrl}/get-pricing/${year}`);
  }
}

标签: angularerror-handling

解决方案


ANSWER FOUND 所以事实证明你不能使用 ! 作为提供者。我用过

    canActivate: [!AfterLoginService],

这就是问题所在。


推荐阅读