Skip to content

Fix navigate with clearHistory to a lazy loaded module #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2017
Merged

Fix navigate with clearHistory to a lazy loaded module #637

merged 1 commit into from
Jan 30, 2017

Conversation

lfabreges
Copy link
Contributor

@lfabreges lfabreges commented Jan 20, 2017

Fix the navigation using extras like clearHistory to a lazy loaded module. Providers, especially NSLocationStrategy were instantiated multiple times.

@patrickpereira
Copy link

Can't wait to have PR! Great job!

Any tips on how to use this PR without having to wait for the repo to be updated?

@lfabreges
Copy link
Contributor Author

lfabreges commented Jan 21, 2017

I've duplicate the module in my app and use it as follows

!! Does not compile with AOT, removed from PR !!

import { NgModule, ModuleWithProviders, NO_ERRORS_SCHEMA } from "@angular/core";
import { RouterModule, Routes, ExtraOptions } from "@angular/router";
import { LocationStrategy, PlatformLocation } from "@angular/common";
import { NSRouterLink } from "nativescript-angular/router/ns-router-link";
import { NSRouterLinkActive } from "nativescript-angular/router/ns-router-link-active";
import { PageRouterOutlet } from "nativescript-angular/router/page-router-outlet";
import { NSLocationStrategy, LocationState } from "nativescript-angular/router/ns-location-strategy";
import { NativescriptPlatformLocation } from "nativescript-angular/router/ns-platform-location";
import { RouterExtensions } from "nativescript-angular/router/router-extensions";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";

export { PageRoute } from "nativescript-angular/router/page-router-outlet";
export { RouterExtensions } from "nativescript-angular/router/router-extensions";
export { NSModuleFactoryLoader } from "nativescript-angular/router/ns-module-factory-loader";
export type LocationState = LocationState;

@NgModule({
    declarations: [
        NSRouterLink,
        NSRouterLinkActive,
        PageRouterOutlet
    ],
    imports: [
        RouterModule,
        NativeScriptModule
    ],
    exports: [
        RouterModule,
        NSRouterLink,
        NSRouterLinkActive,
        PageRouterOutlet
    ],
    schemas: [NO_ERRORS_SCHEMA]
})
export class CustomRouterModule {
    static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders {
        return {
            ngModule: CustomRouterModule,
            providers: [
                NSLocationStrategy,
                { provide: LocationStrategy, useExisting: NSLocationStrategy },
                NativescriptPlatformLocation,
                { provide: PlatformLocation, useClass: NativescriptPlatformLocation },
                RouterExtensions,
                ...RouterModule.forRoot(routes, config).providers
            ]
        };
    }

    static forChild(routes: Routes): ModuleWithProviders {
        return {
            ngModule: CustomRouterModule,
            providers: RouterModule.forChild(routes).providers
        };
    }
}

@hdeshev
Copy link
Contributor

hdeshev commented Jan 27, 2017

This looks great, but unfortunately it breaks AoT compilation for me:

~/t/test-router $ ./node_modules/.bin/ngc -p tsconfig.aot.json 
Can't resolve all parameters for provideLocationStrategy in /home/local/TELERIK/hdeshev/tmp/test-router/node_modules/@angular/router/src/router_module.d.ts: ([object Object], ?, [object Object]).

The error goes away if I try the master branch. Any ideas?

@lfabreges
Copy link
Contributor Author

It seems that the compiler is unable to resolve the @Inject(APP_BASE_HREF) parameter,. According to this thread angular/angular#8623, deps should't use the new Inject() format but instead directly reference the token. I don't know why they still do it that way in the RouterModule.

It causes the compiler to flag the dependency with hasUnknowDeps in the _getDependenciesMetadata function of compiler/src/metadata_resolver.ts. paramEntry is just an object since not decorated and thus not recognized by the instanceof Inject.

@lfabreges lfabreges closed this Jan 28, 2017
@lfabreges
Copy link
Contributor Author

I've find another way, will do PR tomorrow if travis is OK

@lfabreges lfabreges reopened this Jan 29, 2017
@tskweres
Copy link

I'm golden, let's get it in!

@danielgek
Copy link

@hdeshev will this get merged in time for 2.5 release?

@hdeshev
Copy link
Contributor

hdeshev commented Jan 30, 2017

Thanks, great work! I tested it with @tskweres's project from #643. Too bad about the router module improvements. :(

@lfabreges, can you squash your commits and I'll try to get them in the upcoming release.

@hdeshev hdeshev merged commit a31b2aa into NativeScript:master Jan 30, 2017
@hdeshev
Copy link
Contributor

hdeshev commented Jan 30, 2017

@danielgek Yes, that's the plan. I hope I can move it to the release branch tomorrow.

@tskweres
Copy link

tskweres commented Feb 3, 2017

#643 with respect to this bug, I'm still seeing the transitions issue. I clone my example repo, update core modules and re-add platform ios and build.

Any thoughts on if 2.5 included the fix? Or am I missing a step?

@tskweres
Copy link

tskweres commented Feb 5, 2017

got it working with tns update next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants