无法解析 app.page.ts 中 AppPage 的所有参数 (?)
Cannot resolve all parameters for AppPage in app.page.ts (?)
这是我的 AppComponent
的代码 -- 请注意,我称它为 AppPage
因为它稍后会演变成 "smart" 组件:
import { Component } from '@angular/core';
import { AuthService } from '@app/core';
@Component({
selector: 'slctr-root',
template: `<router-outlet></router-outlet>`
})
export class AppPage {
constructor(private authService: AuthService) {
authService.handleAuthentication();
}
}
在WebStorm中,@Component
下划线错误如下:Cannot resolve all parameters for AppPage in app.page.ts (?)
我的 tsconfig.json
文件有 "emitDecoratorMetadata": true
和 "lib": [ "es2017", "dom" ]
并且应用程序构建正确。
我错过了什么?
更新:显示 CoreModule
和 AppModule
代码:
@NgModule({
imports: [ CommonModule ],
declarations: [LandingPageComponent],
providers: [
// App singleton services
AuthService
],
exports: [
// 3rd party libraries
// Components
LandingPageComponent
]
})
export class CoreModule {
// make sure CoreModule is imported only by one NgModule: the AppModule
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error('CoreModule is already loaded. Please import it in the AppModule only.');
}
}
}
@NgModule({
declarations: [
AppPage
],
imports: [
BrowserModule,
AppRoutingModule,
CoreModule
],
providers: [],
bootstrap: [AppPage]
})
export class AppModule {
}
更新: 添加 AuthService
代码:
import { Injectable } from '@angular/core';
import { environment } from '@env/environment';
import * as auth0 from 'auth0-js';
@Injectable()
export class AuthService {
auth0 = new auth0.WebAuth({
clientID: environment.auth0.clientId,
domain: environment.auth0.domain,
responseType: 'token id_token',
audience: environment.auth0.audience,
redirectUri: environment.auth0.redirectUri,
scope: 'openid email profile'
});
public static loggedIn(): boolean {
// Check whether the current time is past the access token's expiry time
return new Date().getTime() < JSON.parse(localStorage.getItem('expires_at'));
}
public login(): void {
this.auth0.authorize({});
}
public handleAuthentication(): void {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
window.location.hash = '';
// Set the time that the access token will expire at
localStorage.setItem('expires_at', JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime()));
localStorage.setItem('id_token', authResult.idToken);
// Route to
} else if (err) {
console.log(err);
}
});
}
}
看起来这是一个已知问题,将在 Angular v5.1.0 中修复:https://github.com/angular/angular/issues/16382
2017 年 12 月 7 日更新: 我确认,这已在 v5.1.0 中修复
这是我的 AppComponent
的代码 -- 请注意,我称它为 AppPage
因为它稍后会演变成 "smart" 组件:
import { Component } from '@angular/core';
import { AuthService } from '@app/core';
@Component({
selector: 'slctr-root',
template: `<router-outlet></router-outlet>`
})
export class AppPage {
constructor(private authService: AuthService) {
authService.handleAuthentication();
}
}
在WebStorm中,@Component
下划线错误如下:Cannot resolve all parameters for AppPage in app.page.ts (?)
我的 tsconfig.json
文件有 "emitDecoratorMetadata": true
和 "lib": [ "es2017", "dom" ]
并且应用程序构建正确。
我错过了什么?
更新:显示 CoreModule
和 AppModule
代码:
@NgModule({
imports: [ CommonModule ],
declarations: [LandingPageComponent],
providers: [
// App singleton services
AuthService
],
exports: [
// 3rd party libraries
// Components
LandingPageComponent
]
})
export class CoreModule {
// make sure CoreModule is imported only by one NgModule: the AppModule
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error('CoreModule is already loaded. Please import it in the AppModule only.');
}
}
}
@NgModule({
declarations: [
AppPage
],
imports: [
BrowserModule,
AppRoutingModule,
CoreModule
],
providers: [],
bootstrap: [AppPage]
})
export class AppModule {
}
更新: 添加 AuthService
代码:
import { Injectable } from '@angular/core';
import { environment } from '@env/environment';
import * as auth0 from 'auth0-js';
@Injectable()
export class AuthService {
auth0 = new auth0.WebAuth({
clientID: environment.auth0.clientId,
domain: environment.auth0.domain,
responseType: 'token id_token',
audience: environment.auth0.audience,
redirectUri: environment.auth0.redirectUri,
scope: 'openid email profile'
});
public static loggedIn(): boolean {
// Check whether the current time is past the access token's expiry time
return new Date().getTime() < JSON.parse(localStorage.getItem('expires_at'));
}
public login(): void {
this.auth0.authorize({});
}
public handleAuthentication(): void {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
window.location.hash = '';
// Set the time that the access token will expire at
localStorage.setItem('expires_at', JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime()));
localStorage.setItem('id_token', authResult.idToken);
// Route to
} else if (err) {
console.log(err);
}
});
}
}
看起来这是一个已知问题,将在 Angular v5.1.0 中修复:https://github.com/angular/angular/issues/16382
2017 年 12 月 7 日更新: 我确认,这已在 v5.1.0 中修复