每次进行惰性加载路由都会在内存中创建新的组件,并且在加载新的相同组件后不会破坏它

时间:2019-03-05 11:46:03

标签: angular2-nativescript

我使用:

  • 本机4.2
  • 角度6.0.0
  • page-router-outlet
  • 通过“延迟加载”(横向导航)在页面之间导航:

    导出const路由= [     {         路径:“”,         loadChildren:“〜/ pages / start-page / start-page.module#StartPageModule”     },
        {         路径:“登录”,         loadChildren:“〜/ pages / login / login.module#LoginModule”     },...

  • 使用具有“ clearHistory = true”的RouterExtensions进行导航

当我从“主页”页面导航到另一页并返回“主页”时,我在内存中看到两个组件“ HomeComponent”。当我重复操作时,它们变为3、4、5,.....

chrome debug window

1 个答案:

答案 0 :(得分:0)

尝试此操作以进行延迟加载路由。

app-routing.module.ts:-

import { NgModule } from "@angular/core";
import { Routes } from "@angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";

const routes: Routes = [
{ path: "", redirectTo: "/login", pathMatch: "full" },
{ path: "startpage", loadChildren: "~/pages/start-page/start-page.module#StartPageModule" },
{ path: "login", loadChildren: "~/pages/login/login.module#LoginModule" }
]

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

重定向代码:-

import { NgZone } from "@angular/core";
import { isIOS } from "tns-core-modules/platform";
import { RouterExtensions } from "nativescript-angular/router";

constructor(private zone: NgZone,
private _routerExtensions: RouterExtensions){ }

gotoStartPage() {
setTimeout(() => {
    this.zone.run(() => {
        this._routerExtensions.navigate(["startpage"], {
            clearHistory: (isIOS) ? true : false,
        });
    });
}, 500);
}