我正在使用离子2开发一个应用程序。侧菜单不会在登录屏幕中。我做到了。但是不知道以下错误会一次又一次地出现。
错误是
由模块" AppModule"导入的意外指令"登录页"。 请添加@ngmodule注释。
在这里我给我代码..
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { LoginPage } from "./login/login";
import { MenuComponent } from "./menu.component";
@NgModule({
declarations: [
MyApp,
HomePage,
ListPage,
MenuComponent
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
LoginPage
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
ListPage,
MenuComponent
],
providers: [{ provide: ErrorHandler, useClass: IonicErrorHandler }]
})
export class AppModule {}
App.component.ts
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { LoginService } from "./login/login.service";
import { LoginPage } from "./login/login";
import { MenuComponent } from "./menu.component";
@Component({
template: '<ion-nav #baseNav></ion-nav>',
})
export class MyApp {
@ViewChild('baseNav') nav: Nav;
constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, private loginService: LoginService) {
this.initializeApp();
}
ngOnInit() {
const componentStack: Array<{page: Component}> = [{
page: MenuComponent
}];
if (!this.loginService.isLoggedIn) {
componentStack.push({ page: LoginPage });
}
this.nav.insertPages(0, componentStack, { animate: false });
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
}
如何解决这个问题...?我正在使用Angular4。
@NgModule({
declarations: [
MyApp,
HomePage,
ListPage,
MenuComponent,
LoginPage // loginPgae will be here not in imports
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
ListPage,
MenuComponent
],
providers: [{ provide: ErrorHandler, useClass: IonicErrorHandler }]
})
在您的代码中,您在@ngmodule中的导入中写下了" loginPage"。而是将其写入声明和入口处。
@NgModule({
declarations: [
MyApp,
HomePage,
ListPage,
MenuComponent,
LoginPage //add LoginPage here
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
ListPage,
MenuComponent,
LoginPage //add LoginPage here
],
providers: [{ provide: ErrorHandler, useClass: IonicErrorHandler }]
})
您必须在声明中声明您的组件。
将组件导入模块的方式是将它们添加到声明中。通常,对于所有组件导入,都是如此。
声明:[ component1, component2, ... ],