简体   繁体   English

Ionic V5 后退按钮不会回到上一个

[英]Ionic V5 back buttons not going back to previous

I'm setting up an ionic application with multiple pages.我正在设置一个具有多个页面的离子应用程序。

I use RouterModule from @angular/router for navigation.我使用来自@angular/router 的RouterModule 进行导航。

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { DetailComponent } from './detail/detail.component';

const routes: Routes = [
  {
    path: "home",
    loadChildren: () =>
      import("./home/home.module").then((m) => m.HomePageModule),
  },
  {
    path: "",
    redirectTo: "home",
    pathMatch: "full",
  },
  {
    path: "detail", component: DetailComponent
  }
];

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

On the main page i got this:在主页上我得到了这个:

<ion-col size="2" class="ion-align-self-end">
    <button type="submit" class="btn btn-primary" [routerLink]="['/detail']">Go</button>
</ion-col>

When i click on the button I navigate to the detailPage and there is my detail component:当我单击按钮时,我导航到 detailPage 并且有我的详细信息组件:

<ion-header translucent>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button ></ion-back-button>
    </ion-buttons>
    <ion-title>Page Two</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content fullscreen class="ion-padding">
  <p>Back-Button is displayed when there is history</p>
</ion-content>

The back button arrow is displayed but when I click on it nothing happens On the inspector i got the ion-router-outlet and it seems ok显示后退按钮箭头,但是当我单击它时,什么也没有发生在检查器上,我得到了离子路由器插座,看起来还可以

<ion-router-outlet>
  <app-home>
  <app-detail>
</ion-router-outlet>

I also tried to use this:我也试过用这个:

<ion-back-button defaultHref="/"></ion-back-button>

Not better:(不是更好:(

I tried using pages instead of just component (using ionic CLI) and it worked just well.我尝试使用页面而不是组件(使用 ionic CLI)并且效果很好。 I don't know why yet, generating pages did also generate routing path like我还不知道为什么,生成页面确实也生成了路由路径

{
    path: 'detail',
    loadChildren: () => import('./detail/detail.module').then( m => m.DetailPageModule)
}

Ionic by default uses LazyLoading, that means, when the route is accessed the Module where the path directs is downloaded.默认情况下,Ionic 使用 LazyLoading,这意味着,当访问路由时,会下载路径指向的模块。

So get used to Route towards Pages(components doesnt have a Module file).所以习惯于向页面路由(组件没有模块文件)。 This is a really good performance trick and the recommended way.这是一个非常好的性能技巧和推荐的方法。

Since Modules define their Component dependencies, then lazy loading helps not to load unneeded dependencies making Angular to be served faster.由于模块定义了它们的组件依赖项,因此延迟加载有助于不加载不需要的依赖项,从而更快地为 Angular 提供服务。

Ion-router-outlet is a wrapper around the Angular router-outlet which adds some animations and a “odd behavior” of caching these Pages. Ion-router-outlet 是 Angular router-outlet 的包装器,它添加了一些动画和缓存这些页面的“奇怪行为”。 While Angular always destroys the previous Component when a new Component is loaded in < router-outlet >, Ionic (when using < ion-router-outlet >) keeps in the DOM the Pages already Loaded.虽然 Angular 总是在 <router-outlet> 中加载新组件时破坏先前的组件,但 Ionic(使用 <ion-router-outlet> 时)将已加载的页面保留在 DOM 中。 This is done to make smooth animations when moving from one Page to the previous one, it can reduce the number of request to the server but the cost is that Pages (and its child Components) already loaded doesnt trigger “ngOnInit” because they are already in the DOM and never gets destroyed.这样做是为了在从一个页面移动到上一个页面时制作流畅的动画,它可以减少对服务器的请求数量,但代价是已经加载的页面(及其子组件)不会触发“ngOnInit”,因为它们已经在 DOM 中并且永远不会被破坏。

Ionic, as said, doesnt destroy Pages but cache them and just uses CSS tricks to “hide” and “show” them.如前所述,Ionic 不会破坏页面,而是缓存它们,只是使用 CSS 技巧来“隐藏”和“显示”它们。 So if you want to force a refresh you would need to use “ionViewWillEnter” and related friends.所以如果你想强制刷新,你需要使用“ionViewWillEnter”和相关的朋友。 Otherwise your Page will never get refreshed.否则您的页面将永远不会刷新。 (Usually in ngOnInit a call to a service is done to perform a request which is binded to the view) (通常在 ngOnInit 中调用服务来执行绑定到视图的请求)

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

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