NullInjectorError:没有AuthenticationService提供程序?

时间:2020-09-07 12:00:06

标签: javascript angular ionic-framework web ionic4

enter image description here

这是身份验证页面:-

import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {SECURITY} from '../constant/SecurityAPI';
import {CONFIG} from '../config';


@Injectable()
export class AuthenticationService {
    constructor(private http: HttpClient) {
    }

    public isUserLoggedIn() {
        return localStorage.getItem('token') === undefined;
    }

    public login(credentials) {

        return new Promise((resolve, reject) => {
            console.log('username ' , credentials.userName, ' pass ' , credentials.password );
            const data = {userName: credentials.userName, password: credentials.password, orgId: CONFIG.ORG_ID};

            this.http.post(SECURITY.GET_LOGIN_URL, JSON.stringify(data), {
                headers: new HttpHeaders().set('Content-Type', 'application/json')
            })
                .subscribe(res => {
                    console.log('res ' , res);
                    resolve(res);
                }, (err) => {
                    reject(err);
                });
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在@Injectable装饰器中指定要提供此服务的位置

@Injectable({
    providedIn: 'root'
})
export class AuthenticationService {
    ...
}

或将其添加到app.module.ts提供者中:

providers: [
  AuthenticationService,
  any other services,
  ...
]