简体   繁体   English

如何在 IONIC 4 中设置 rootPage

[英]How to set rootPage in IONIC 4

This is what i have now and really can't seem to find the right answer to my question i've looked everywhere so maybe someone that knows can help me?.这就是我现在所拥有的,似乎真的无法找到我四处寻找的问题的正确答案,所以也许知道的人可以帮助我? How do i set RootPage like in ionic 3 into Ionic 4 because i tried everything and this is what is left with trying我如何将 RootPage 像 ionic 3 一样设置为 Ionic 4,因为我尝试了所有方法,这就是剩下的尝试

import {Component, ViewChild} from '@angular/core';
import {NavController, Platform} from '@ionic/angular';
import {SplashScreen} from '@ionic-native/splash-screen/ngx';
import {StatusBar} from '@ionic-native/status-bar/ngx';
import {Router} from '@angular/router';
import {GettingStartedPage} from './getting-started/getting-started.page';

@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
})

export class AppComponent {
@ViewChild(NavController) nav: NavController;
rootPage;


constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private NavCtrl: NavController) {
    this.initializeApp();
}

// Eindigt constructor

initializeApp() {
    this.platform.ready().then(() => {
        this.splashScreen.hide();
        // set status bar naar onze app kleur
        this.statusBar.backgroundColorByHexString('#E84B56');
    });
}

openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.rootPage.navigateRoot('/getting-started');

}
}

Since Ionic 4 you use the Angular Routing.从 Ionic 4 开始,您使用 Angular Routing。 If you want /getting-started to be your rootpage, go into the app-routing.module.ts and edit the routes.如果您希望/getting-started成为您的根页面,请进入app-routing.module.ts并编辑路由。 If you don't have on, create a Route that points to your Page and redirect the one with path: '' to /getting-started .如果您没有打开,请创建一个指向您的页面的路由,并使用path: ''将其重定向到/getting-started

So a final app-routing.module.ts could be:所以最终的app-routing.module.ts可能是:

const routes: Routes = [
    {path: '', redirectTo: 'getting-startet', pathMatch: 'full'}, //Root Path redirect to your getting-started path
    {path: 'getting-started', loadChildren: './getting-started/getting-started.module#GettingStartedPageModule'}, // when hit the getting-startet path, load the page
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule {
}

I hope the following helps you:我希望以下内容对您有所帮助:

constructor(private nav: NavController){}

private setRoot(){
  this.nav.navigateRoot('/getting-started');
}

Ionic 4 uses routes to navigate. Ionic 4 使用路由来导航。 You can find configuration for routes in /src/app/app-routing.module.ts , even though you can still set it up in your app.module.ts file.您可以在/src/app/app-routing.module.ts找到路由的配置,即使您仍然可以在app.module.ts文件中进行设置。 Quoting the Ionic 4 documentation: , the most basic configuration looks a bit like this:引用Ionic 4 文档: ,最基本的配置看起来有点像这样:

import { RouterModule } from '@angular/router';

@NgModule({
  imports: [
  ...
  RouterModule.forRoot([
    { path: '', component: LoginComponent },
    { path: 'detail', component: DetailComponent },
  ])
  ],
})

The simplest breakdown for what we have here is a path/component lookup.我们这里最简单的细分是路径/组件查找。 When our app loads, the router kicks things off by reading the URL the user is trying to load.当我们的应用加载时,路由器通过读取用户试图加载的 URL 来启动。 In our sample, our route looks for '', which is essentially our index route.在我们的示例中,我们的路由寻找 '',它本质上是我们的索引路由。 So for this, we load the LoginComponent.因此,为此,我们加载 LoginComponent。 Fairly straight forward.非常坦率的。

If you want to load a different component on initial load, you should use redirects如果你想在初始加载时加载不同的组件,你应该使用重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM