You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a app where i need to have a logic in routing to decide which page should be the home page, it works fine with JIT but with AOT. Here is how my code looks like
Thanks @deebloo, if you look at my example code i'm getting subdomain from the url and then in routes loading relevant lazy module. somehow i need to do this in routes.
its a funny question but let me ask you, is there not any way to compile only routes with JIT and all other in AOT? I don't think its possible but let me know what you think?
I save routes in my format that I need because of route localization.
Then I have a function that translates my format to the array of routes router understands.
Looks it can't work with AOT like that.
Any workaround?
Reacted by Andrea Dessi, Benjamin Schäublin and Germán Schuager
Similar problem here... I have dynamic yet synchronous routes that have to be possible to turn off in config file. I couldn't filter them right in definition due to "function calls are not supported" error and I finally did it in my module constructor by operating on router.config... The routes are added properly and work in JiT and I think they was also working in AoT last time, but now they do not. Any workaround or better solution to make it work in AoT compilation?
Edit: My problem was caused by code minification, not strictly by AoT compiler. I compared component names to find specific route and it found other one with the same name in AoT. After fixing that, my dynamically filtered routes work as expected.
@DanielKucal What do you mean by you found another route with the same name in AoT? Can you please explain this to me? I have the same problem, dynamic routes from file are throwing an exception .. Thank you for you help!
@raujonas, if I remember that correctly I was comparing class names (which are being replaced during minification). The solution was to add a static field to classes and compare it while matching the routes instead.
Thank you @lpikora, very nice of your solution.
Also I found some obvious limitations in AOT mode, the fact is we can only change route configure with router.resetConfig(), but we can't bring new route configure like a new lazy module at runtime, it will report error
Angular AOT engine goes through original route configure tree to find components and modules, compile them into ngfactory at compilation time. Any stuff new at runtime will be reported as missing ngfactory by Angular.
And also I have not try it in Angular universal server-side rendering mode.
@unspike I guess No, there is no ideal approach, since Angular AOT means all the route configurations must be laid down at compile time, not at run time.
As long as there is no new route added during runtime, we can adopt @lpikora 's approach.
If there is business requirement asks for such dynamic route to be created on-the-fly, then that route can not be a part of the original AOT appliaction.
My project does not ask me to have dynamic route on-the-fly, but I does hope we can resuse business route modules as much as possible cross different products like libraries, so we kinda use a tool to read our customized project configuration and have build script to rewrite TS source file before AOT build runs. As a workaround, quite like a Angular command line plugin.
Activity
deebloo commentedon Mar 30, 2017
For AOT your routes need to be statically analyzable. I'd your need dynamic values in your routes can you use route params instead?
sam-rusty commentedon Mar 30, 2017
Thanks @deebloo, if you look at my example code i'm getting subdomain from the url and then in routes loading relevant lazy module. somehow i need to do this in routes.
its a funny question but let me ask you, is there not any way to compile only routes with JIT and all other in AOT? I don't think its possible but let me know what you think?
deebloo commentedon Mar 30, 2017
You can't do that in the routes. It looks like the only concern here is the actual name of the routes? and no it is not possible
sam-rusty commentedon Mar 31, 2017
Okay thank you so much @deebloo, i'm gonna close this issue.
hheexx commentedon Apr 4, 2017
I am having the same problem.
I save routes in my format that I need because of route localization.
Then I have a function that translates my format to the array of routes router understands.
Looks it can't work with AOT like that.
Any workaround?
crizstian commentedon Apr 11, 2017
@iridiumsoft
Does lazy loading working well for you ?
how is your configuration i am having problems with lazy loading
markoddi commentedon Apr 13, 2017
I cant get my routes working in AOT either. Works fine in JIT with ng serve
markoddi commentedon Apr 13, 2017
These are my routes that only don't work in AOT:
import { Route} from '@angular/router';
import { HomeComponent } from './home.component';
export const HomeRoutes: Route[] = [
{
path: '',
component: HomeComponent
},
{
path: 'home',
component: HomeComponent
}
];
yy7054wyq5 commentedon Jun 14, 2017
So if use dynamic routes must close AOT...
lpikora commentedon Jun 21, 2017
This can be solved by providing routes definition to
ROUTESusing function factory... (tested on Angular 4.1.2)Modify your AppModule (app.module.ts):
change RouterModul.forRoot in imports array
to
add this two providers in providers array:
define factory function (at top of the app.module.ts file):
maybe you need also create new file eg:
app.routes.tsand put your routes definitions thereand import it in AppModule (app.module.ts).
DanielKucal commentedon Aug 15, 2017
Similar problem here... I have dynamic yet synchronous routes that have to be possible to turn off in config file. I couldn't filter them right in definition due to "function calls are not supported" error and I finally did it in my module constructor by operating on
router.config... The routes are added properly and work in JiT and I think they was also working in AoT last time, but now they do not. Any workaround or better solution to make it work in AoT compilation?Edit: My problem was caused by code minification, not strictly by AoT compiler. I compared component names to find specific route and it found other one with the same name in AoT. After fixing that, my dynamically filtered routes work as expected.
raujonas commentedon Jun 21, 2018
@DanielKucal What do you mean by you found another route with the same name in AoT? Can you please explain this to me? I have the same problem, dynamic routes from file are throwing an exception .. Thank you for you help!
DanielKucal commentedon Jul 2, 2018
@raujonas, if I remember that correctly I was comparing class names (which are being replaced during minification). The solution was to add a static field to classes and compare it while matching the routes instead.
liujingbreak commentedon Nov 14, 2018
Bad news in 2018,
{provide: ROUTES, multi: true, useFactory: getRoutes}does not work today.fix(compiler-cli): ngtools should accept
useFactory(lazy routes)lpikora commentedon Nov 14, 2018
Solution for Angular 6+
configure your routes as usual:
then change routes array as desired and reset routes at app runtime (in app.component.ts contsructor)
It also works with lazyloaded routes.
liujingbreak commentedon Dec 26, 2018
@lpikora Does it work in AOT mode? I am guessing it is not.
lpikora commentedon Dec 26, 2018
@liujingbreak yes, it works with AOT. The key is using
router.resetConfig(routes)with customisedroutesarray in runtime.liujingbreak commentedon Jan 8, 2019
Thank you @lpikora, very nice of your solution.
Also I found some obvious limitations in AOT mode, the fact is we can only change route configure with
router.resetConfig(), but we can't bring new route configure like a new lazy module at runtime, it will report errorAngular AOT engine goes through original route configure tree to find
components andmodules, compile them intongfactoryat compilation time. Any stuff new at runtime will be reported as missingngfactoryby Angular.And also I have not try it in Angular universal server-side rendering mode.
unspike commentedon Feb 13, 2019
@liujingbreak have you found any possible approach to make it works with AOT?
liujingbreak commentedon Feb 22, 2019
@unspike I guess No, there is no ideal approach, since Angular AOT means all the route configurations must be laid down at compile time, not at run time.
As long as there is no new route added during runtime, we can adopt @lpikora 's approach.
If there is business requirement asks for such dynamic route to be created on-the-fly, then that route can not be a part of the original AOT appliaction.
My project does not ask me to have dynamic route on-the-fly, but I does hope we can resuse business route modules as much as possible cross different products like libraries, so we kinda use a tool to read our customized project configuration and have build script to rewrite TS source file before AOT build runs. As a workaround, quite like a Angular command line plugin.
surfjedi commentedon May 1, 2019
Any one have any updates? I need this too?
mayrmax commentedon Jul 23, 2019
I got it working using the ROUTES injection token.
So it seems to still be possible with NG 8.1.
angular-automatic-lock-bot commentedon Sep 9, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.