Dostałem ten błąd podczas uruchamiania aplikacji jonowej Nie jestem pewien, co robię, ale próbuję uzyskać uwierzytelnianie Login API JWT w jonowym

Funkcja kanalizacji była jak w przypadku błędu, więc zmieniam go.

CanActivate(): boolean {
    return this.authService.isAuthenticated();
  }
  NullInjectorError: No provider for Storage!
NullInjectorError: R3InjectorError(LoginPageModule)[AuthenticationService -> AuthenticationService -> Storage -> Storage -> Storage]: 
  NullInjectorError: No provider for Storage!
    at NullInjector.get (core.js:915)
    at R3Injector.get (core.js:11082)
    at R3Injector.get (core.js:11082)
    at R3Injector.get (core.js:11082)
    at injectInjectorOnly (core.js:801)
    at ɵɵinject (core.js:805)
    at Object.AuthenticationService_Factory [as factory] (ɵfac.js? [sm]:1)
    at R3Injector.hydrate (core.js:11249)
    at R3Injector.get (core.js:11071)
    at NgModuleRef$1.get (core.js:24199)
    at resolvePromise (zone-evergreen.js:798)
    at resolvePromise (zone-evergreen.js:750)
    at zone-evergreen.js:860
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:27425)
    at ZoneDelegate.invokeTask (zone-evergreen.js:398)
    at Zone.runTask (zone-evergreen.js:167)
    at drainMicroTaskQueue (zone-evergreen.js:569)

Mój kod usługi uwierzytelniania:

import { Platform } from '@ionic/angular';
import { BehaviorSubject } from 'rxjs';
import { Storage } from '@ionic/Storage'; 
import { HttpClient } from '@angular/common/http';
const TOKEN_KEY ='auth-token';
@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {
  authenticationState = new BehaviorSubject(false)
  constructor( private storage: Storage, private plt: Platform, private http: HttpClient) {
    this.plt.ready().then(()=>{
    this.chechToken();
    });
   }
    login(user){
      this.http.post('http:127.0.0.1:8001/api/user', user).subscribe(res =>{
        return this.storage.set(TOKEN_KEY,`Bearer ${res['token']}`).then( res =>{
          this.authenticationState.next(true);
        });
      });
    }
    logout(){
      return this.storage.remove(TOKEN_KEY).then ( () =>{
        this.authenticationState.next(false);
      });
    }
    isAuthenticated(){
        return this.authenticationState.value;
    }
    chechToken(){
      return this.storage.get(TOKEN_KEY).then ( res =>{
        if (res){
          this.authenticationState.next(true);
        }
      });
    }
}

Moja usługa strażnika

import { AuthenticationService } from './authentication.service';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
  constructor(private authService: AuthenticationService) { 
  }
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
    return this.authService.isAuthenticated();
  }
}

Mój kod modułu aplikacji

import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Storage } from '@ionic/storage';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

To jest samouczek: https://www.youtube.com/watch?v = O35CY5ZYU0 & AMP; T = 3S

1
amina ibrahim 29 październik 2020, 10:43

1 odpowiedź

Najlepsza odpowiedź

Czy dostarczyłeś IonicStorageModule za pomocą FORROOT w aplikacji Appmodule?

Zobacz Docs:

@NgModule({
  declarations: [
    // ...
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    // Without this, Storage won't be available!
    IonicStorageModule.forRoot()
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // ...
  ],
  providers: [
    // ...
  ]
})
export class AppModule {}
1
code-gorilla 29 październik 2020, 07:52