【问题标题】:No provider for NavController! Ionic 3没有 NavController 的提供者!离子 3
【发布时间】:2018-05-19 21:38:39
【问题描述】:

我正在为我的大学项目使用Ionic3,我添加了我的app.html 的设置部分并尝试链接(click)="home()",但这对我不起作用。

我收到以下错误

错误:没有导航控制器的提供程序! 在 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) 在 resolveNgModuleDep (http://localhost:8100/build/vendor.js:9847:25) 在 NgModuleRef_.get (http://localhost:8100/build/vendor.js:10935:16) 在 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);
  }
}

【问题讨论】:

    标签: ionic-framework


    【解决方案1】:

    好吧,你为什么要尝试导入 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);
    

    【讨论】:

    • 也试试这个解决方案
    • 导航未定义。我将您的代码添加到 app.component.ts (ionic 3)
    • 使用nav时导航不可用,用户无法返回!
    • @KhurshidAnsari 你什么时候打电话给导航?在 ng init 上的构造函数中?
    • @volodymyr-bilyachat 我解决了参数。我正在使用:this.nav.push(page, paramObj) 并且可以使用!
    【解决方案2】:

    尝试使用getActiveNav()

    这样,

    import {App} from 'ionic-angular';
    
    constructor(public app: App){}
    
    login() { 
        this.app.getActiveNav().setRoot(Home); 
    }
    

    【讨论】:

    • 您应该声明他们需要从构造函数中删除 NavController
    • 这是您更新的代码吗?没有 getActiveNav()
    • @VolodymyrBilyachat 为什么?即使你已经导入它也不会抛出任何错误
    • @Gowtham 我认为如果您尝试注入它会引发错误。是的,如果你只是导入它就可以了,但如果你注入它就会失败。
    • (getActiveNav) 已弃用,将在下一个主要版本中删除。请改用 getActiveNavs。
    【解决方案3】:

    我已经通过以下代码在 ionic 3 中解决了这个问题。

    import {App} from 'ionic-angular';
    
    constructor(public app: App){}
    
    login() { 
        this.app.getActiveNav().push(XxxPage);
    }
    

    【讨论】:

      【解决方案4】:

      根据Ionic v3 docs

      您不能注入 NavController,因为作为导航控制器的任何组件都是根组件的子组件,因此它们不能被注入。

      您需要从构造函数中删除 NavController,因为它不能被注入,并且尝试这样做会导致您面临的错误。

      相反,您应该在应用模板上有一个&lt;ion-nav&gt; 组件,并使用@ViewChild 指令在AppComponent 的代码隐藏中获取其实例。

      【讨论】:

        【解决方案5】:

        正如您在 ionic api docs 中看到的,您必须在构造函数中加载 NavController

        constructor(private navCtrl: NavController) {}

        【讨论】:

        • 嗯,是的,当您更新了我可以看到的代码时...似乎 app.component.ts 页面中没有 NavController 提供程序,您只是在其中呈现 &lt;ion-nav&gt;。正如 Volodymyr 提到的,尝试使用 ViewChild 对象。
        猜你喜欢
        • 2018-09-22
        • 2017-03-01
        • 2018-01-18
        • 2016-11-02
        • 1970-01-01
        • 2018-11-27
        • 2018-07-28
        • 2018-05-09
        • 2017-08-13
        相关资源
        最近更新 更多