我在我的大学项目中使用Ionic3
,我添加了app.html
的设置部分,并尝试关联(click)="home()"
,但它对我不起作用
我收到了以下错误
错误:没有NavController的提供程序! at injectionError(http://localhost:8100/build/vendor.js:1527:90) 在noProviderError(http://localhost:8100/build/vendor.js:1565:12) 在ReflectiveInjector _。 throwOrNull(http://localhost:8100/build/vendor.js:3007:19) 在ReflectiveInjector 。 getByKeyDefault(http://localhost:8100/build/vendor.js:3046:25) 在ReflectiveInjector 。 getByKey(http://localhost:8100/build/vendor.js:2978:25) 在ReflectiveInjector .get(http://localhost:8100/build/vendor.js:2847:21) at resolveNgModuleDep(http://localhost:8100/build/vendor.js:9847:25) 在NgModuleRef_.get(http://localhost:8100/build/vendor.js:10935:16) at resolveDep(http://localhost:8100/build/vendor.js:11438:45) 在createClass(http://localhost:8100/build/vendor.js:11302:32)
app.html
<!--------------------------------------Main Navigation--------------------------->
<ion-menu id="myMenu" [content]="content" side="right" persistent="true" [class]="selectedTheme">
<ion-header>
<ion-toolbar>
<ion-grid>
<ion-row>
<ion-col col-6>
<div text-left style="" class="app-listname">
Project</div>
</ion-col>
<ion-col (click)="home()">
<div class="tether-setting" style="text-align: right;font-size: 2rem; color:#a57958;">
<ion-icon ios="ios-settings-outline" md="md-settings"></ion-icon>
</div>
</ion-col>
<ion-col>
<div class="tether-logout" style="font-size: 2rem; color:#a57958; text-indent: 0rem; text-align: right;">
<ion-icon ios="ios-log-out" md="md-log-out"></ion-icon>
</div>
</ion-col>
</ion-row>
</ion-grid>
</ion-toolbar>
</ion-header>
<ion-content>
<div class="ion-tm">
<ion-list>
<button menuClose ion-item *ngFor="let p of pages" [class.activeHighlight]="checkActive(p)" (click)="openPage(p)">
<ion-icon [name]="p.icon" item-left></ion-icon>{{p.title}}</button>
</ion-list>
</div>
</ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false" [class]="selectedTheme"></ion-nav>
<!--------------------------------------Main Navigation--------------------------->
app.component.ts
import {Component, ViewChild} from '@angular/core';
import {Nav, Platform,NavController} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';
import {HomePage} from '../pages/home/home';
import {SettingsPage} from "../pages/settings/settings";
import {HelpPage} from "../pages/help/help";
import {UserloginslidePage} from "../pages/userloginslide/userloginslide";
import {SettingsProvider} from "../providers/settings/settings";
import {ModalPage} from "../pages/modal/modal";
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = UserloginPage;
selectedTheme: String; //Themeoption
activepage: any;
pages: Array<{ title: string, component: any, icon: string }>;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private settings: SettingsProvider,private navCtrl: NavController) {
this.settings.getActiveTheme().subscribe(val => this.selectedTheme = val); //Themeoption
platform.ready().then(() => {
statusBar.styleDefault();
splashScreen.hide();
});
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
});
this.pages = [
{title: 'Dashboard', component: HomePage, icon: "ios-home-outline"},
{title: 'Settings', component: SettingsPage, icon: "ios-settings-outline"},
{title: 'Help', component: HelpPage, icon: "ios-help-circle-outline"},
{title: ' User loginslide ', component:UserloginslidePage, icon: "ios-contact-outline"},
];
this.activepage = this.pages[0];
}
//Themeoption
openPage(page) {
this.nav.setRoot(page.component);
this.activepage = page;
}
checkActive(page) {
return page == this.activepage;
}
home(){
this.navCtrl.push(HomePage);
}
}
答案 0 :(得分:33)
那么,为什么要尝试导入NavController?您有@ViewChild(Nav) nav: Nav;
可以使用它。
你必须删除 NavController 以免在构造函数中注入
替换你的行
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private settings: SettingsProvider,private navCtrl: NavController) {
带
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private settings: SettingsProvider) {
然后替换
行 this.navCtrl.push(HomePage);
带
this.nav.push(HomePage);
答案 1 :(得分:28)
尝试使用getActiveNav()
像这样,
import {App} from 'ionic-angular';
constructor(public app: App){}
login() {
this.app.getActiveNav().setRoot(Home);
}
答案 2 :(得分:0)
我已经通过以下代码在ionic 3中解决了这个问题。
import {App} from 'ionic-angular';
constructor(public app: App){}
login() {
this.app.getActiveNav().push(XxxPage);
}
答案 3 :(得分:0)
您不能注入NavController,因为任何作为导航控制器的组件都是根组件的子代,因此它们无法被注入。
您将需要从构造函数中删除NavController,因为它无法注入,并且尝试这样做将导致您面临的错误。
相反,您应该在应用模板上拥有一个<ion-nav>
组件,并使用@ViewChild指令在AppComponent的代码隐藏中获取其实例。
答案 4 :(得分:-8)
正如您在离子api docs中看到的那样,您必须在构造函数中加载NavController
:
constructor(private navCtrl: NavController) {}