-
-
Notifications
You must be signed in to change notification settings - Fork 241
Is possible to use dynamic routing(lazy loading) feature of Angular2 in NativeScript + Angular2 #533
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
Comments
Hey @wantaek Yes, you can use lazy loading for routes - We are currently working on the lazy loading implementation in this branch of the nativescript-sdk-examples-ng. Once our implementation is ready, tested and clean we will update the master branch as well and you can use it as a reference for out project. I will update the info here once we have a final solution. |
@NickIliev, I saw that there was a commit (#530) to support lazy loading for the page-router-outlet. My use case is for lazy loading components using the standard router-outlet. It seems that @wantaek indicated that this was his use case also based on the example template he included even though he mentioned 'lazy' page. Will this also be supported? |
Hey @fricker Yes, you can already use router-outlet with lazy loading. e.g in app.component.ts.:
you can change it like this:
Both animations and button are sections which leads to three more sub-sections for which we are using lazy loading. If you open app/ui-category/button you will notice a module file and a component file . In button-example.module.ts we are lazy loading the childrens of Button example like this:
Note that here we are using forChild method with NativeScriptRouterModule. The final part where we are actually loading those children is placed in app.routes.ts.
Again the whole architecture is virtually the same for page-router-outlet and for router-outlet except for the actual place where you are placing the outlets (in our case the app.component.ts) |
Thank you very much for providing some much needed clarity to how loadChildren is supported in nativescript-angular. I was not aware Angular router's loadChildren could be configured using a call to require wrapped in a function. The Angular documentation that I have been referencing makes no mention of this. Additionally, there is no mention in the NativeScript documentation that loadChildren was supported or how it needs to be configured. When I tried using the string syntax indicated in Angular's docs and got the same System not found error that @wantaek encountered. I even tried installing webpack to see if that might work. Now that I know that this alternate loadChildren configuration exists I went back to the web implementation of my app and changed it to use the wrapped require() implementation to maintain some consistency. BTW, your link to the nativescript-sdk-examples-ng repo in your initial reply to @wantaek is broken. I'll definitely be using that project to supplement the NativeScript docs going forward. |
@fricker Good to know that I've managed to help you guys! I have updated the link to our sample app - the lazy load is now implemented in the master branch. |
Hi NickIliev,
And it doesn't seem to work. As loadChildren is called asynchronously in your example, canActivate seems to resolve before and although the redirect I have in my AuthGuard component is fired correctly, the loadChildren seems to bypass my redirection. Any idea how to make it work?
|
Hey @astrobob you can use canActivateChild For example, I have tried it in our nativescript-sdk-examples-ng application like this:
note that the above implementation is defaulted to return false (the route is not activated) |
Hi @NickIliev Thank you very much! It works perfectly! In didn't know about CanActivateChild For those who want to see the code difference, I put below: in my app.router:
in my auth.guard.ts (that I import in my app.router.ts)
Cheers |
For those of you who googled for Angular Router lazy loading and is wondering why you can't just use string-based routes like import { NSModuleFactoryLoader } from 'nativescript-angular/router';
@NgModule({
...
providers: [
{ provide: NgModuleFactoryLoader, useClass: NSModuleFactoryLoader }
]
})
export class AppModule { }
|
@otaran remember to import NgModuleFactoryLoader
|
Uh oh!
There was an error while loading. Please reload this page.
From @wantaek on November 14, 2016 8:42
Did you verify this is a real problem by searching [Stack Overflow]
Yes
Tell us about the problem
I want to use dynamic routing(lazy loading) feature of Angular2 in NativeScript + Angular2
This is a dynamic routing(lazy loading) of Angular2 sample source.
http://plnkr.co/edit/fBqrEpqafiVSB2Vvapzb?p=preview
I use this source to my NativeScript + Angular2 project.
But when i try to navigate 'lazy' page, error occurred.
CONSOLE ERROR file:///app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:344:22: Error: Uncaught (in promise): ReferenceError: Can't find variable: System
Which platform(s) does your issue occur on?
I test in iOS. It may occur both.
Please provide the following version numbers that your issue occurs with:
tns --version
to fetch it)2.4.0
node_modules/tns-core-modules/package.json
file in your project)2.4.0
"tns-android"
and"tns-ios"
properties in thepackage.json
file of your project)2.4.0
package.json
file of yourproject)
Please tell us how to recreate the issue in as much detail as possible.
When I try to navigate to 'lazy' page, error ocurred:
CONSOLE ERROR file:///app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:344:22: Error: Uncaught (in promise): ReferenceError: Can't find variable: System
Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.
Copied from original issue: NativeScript/NativeScript#3085
The text was updated successfully, but these errors were encountered: