没有NavController的提供者!离子3

cor*_*114 9 ionic-framework

我正在使用Ionic3我的大学项目,我添加了我的部分设置app.html并试图链接(click)="home()"但它不适合我.

我收到了以下错误

错误:没有NavController的提供程序!atjectionError(http:// localhost:8100/build/vendor.js:1527:90)at ReflectorInjector_的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).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)在resolveDep的NgModuleRef_.get(http:// localhost:8100/build/vendor.js:10935:16)(http:// localhost:8100/build/vendor.js:11438:45)at 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--------------------------->
Run Code Online (Sandbox Code Playgroud)

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

Vol*_*hat 36

那你为什么要尝试导入NavController?你有 @ViewChild(Nav) nav: Nav;可以使用它.

必须从注入构造函数中删除NavController

替换你的线

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private settings: SettingsProvider,private navCtrl: NavController) {
Run Code Online (Sandbox Code Playgroud)

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private settings: SettingsProvider) {
Run Code Online (Sandbox Code Playgroud)

然后换行

 this.navCtrl.push(HomePage);
Run Code Online (Sandbox Code Playgroud)

 this.nav.push(HomePage);
Run Code Online (Sandbox Code Playgroud)

  • 导航未定义.我在app.component.ts中添加你的代码(离子3) (3认同)

Gow*_*ham 29

尝试使用 getActiveNav()

像这样,

import {App} from 'ionic-angular';

constructor(public app: App){}

login() { 
    this.app.getActiveNav().setRoot(Home); 
}
Run Code Online (Sandbox Code Playgroud)