Skip to content
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

Angular2 router : Changing route doesn't load the component for first time #8166

Closed
devin6391 opened this issue Apr 21, 2016 · 22 comments
Closed

Comments

@devin6391
Copy link

I am changing the route of angular2 application from '/' to '/xyz'. For 'xyz' I have made a component named XYZComponent. This XYZComponent implements a hook for router lifecycle named "CanActivate". In this hook I am changing the title of the page for new route.
@CanActivate((next, prev) => {
document.title = "mysite | xyz"
return true;
})
Then I implement OnInit for this component and call the method "ngOnInit()". In this method I am calling the server to give me some data. This data is stored in a variable named xyzData, which is binded to the template. My template is:

<div class="xyzComp">
 <p>{{xyzData.msg}}</p>
</div>

OnInit is:

ngOnInit(){
    console.log("xyz component init");
    this._myRestfulService.getXyz()
        .subscribe(data => {
            console.log("success",data);
            this.xyzData = data;
        }, err => {
            this.xyzData = {msg:"cannot get data"};
            console.log("error", err);
        });
}

Changing the route is done by calling _route.navigate(["XYZ"]) from the click of a button named xyzBtn, which is present in the header of my app. The header is constant for all the routes.
Problem is that when I click on xyzBtn for first time it calls only whatever written in @CanActivate decorator of XYZComponent. Next time when I click xyzBtn,@CanActivate is not called but this time ngOnInit() is called.
Please help me on this. This problem occurs only when I deploy my code on server. The deployment server is NGINX server. On nginx server we have configured to ignore frontend routes.
However my code works perfectly well on development server which is a npm module named lite-server.

@zoechi
Copy link
Contributor

zoechi commented Apr 21, 2016

call the method "ngOnInit()".

sounds weird. I think this requires at least a Plunker that allows to reproduce.

GitHub issues are for bug reports and feature requests.
For support questions please use other channels like the ones listed in CONTRIBUTING - Got a Question or Problem?

@devin6391
Copy link
Author

I think its a bug of Angular2. It might be crashing while deciding which lifecycle hook to address first - router or component. The documentation of CanActivate lifecycle hook of router is also not very clear on how it interacts with lifecycle of a component.

@zoechi
Copy link
Contributor

zoechi commented Apr 21, 2016

Would probably be easy to know if you would provide a Plunker that demonstrates what you're doing.

@vaudy
Copy link

vaudy commented Apr 21, 2016

I think ngOnInit is called for components only

@devin6391
Copy link
Author

Plunker : https://plnkr.co/edit/E4HNXEjaIP7th77UE4dK?p=preview

@brandonroberts
Copy link
Contributor

@devin6391 that plunker doesn't work: http://plnkr.co/edit/oZzjAH34WJ1R14z0Xtbl?p=preview

@devin6391
Copy link
Author

One more thing, I am making a bundle file from systemjs builder before deploying. Could that raise issue?

@brandonroberts
Copy link
Contributor

It shouldn't. Usually if your lifecycle hooks aren't being called until you manually trigger some action, your polyfills aren't setup correctly. Are you including the angular2 polyfills in a script tag in your index.html on deployment?

@zoechi
Copy link
Contributor

zoechi commented Apr 21, 2016

@devin6391
Copy link
Author

Hi, I have also done some debugging. The areas where I find Angular2 incompatible are:

  1. Cannot do minification. We have to use .dev version. This need to be fixed.
  2. The doc of Angular2 doesn't explain how routing lifecycle affect component lifecycle and vice versa.

Few problems are there with system js also. Although this is not the place to post SystemJs issues but there is some kind of problem when system js bundles up the angular files in module.

I think with time these problems will get resolved, and we are definitely waiting for a stable version-1 of angular2

@zoechi
Copy link
Contributor

zoechi commented Apr 22, 2016

@ribizli
Copy link

ribizli commented May 6, 2016

I had the same problem, and after ca. 4 hours investigation I've found that the problem was caused by systemjs/dist/system-polyfills.js. I removed this dependency and it works now at least in Chrome.

I need to add one more details: the component's template was using *ngIf, so it was something related to change detection I assume.

Here is a plunkr to reproduce: http://plnkr.co/edit/X8cXgf9GhfLZlVmo2fAK (you have to click the route link twice to get the component rendered)
Just comment the system-polyfills in index.html to get it work...
Works also if the *ngIf is removed from the components.

@jpleclerc
Copy link

jpleclerc commented May 6, 2016

@ribizli I removed system-polyfills.js and it solved the problem for me too. ty!

@JSMike
Copy link

JSMike commented May 11, 2016

Thx, I couldn't figure out why my component lifecycle wasn't starting, removing the system-polyfill.js worked for me as well.

@mohyeid
Copy link

mohyeid commented Jun 24, 2016

Removing system-polyfills.js worked for me

@after-ephemera
Copy link

I am having this same issue, but using webpack and angular2-universal. Any ideas of what could be happening?

@kwgainey
Copy link

kwgainey commented Aug 9, 2016

+1 for removing system-polyfills.js. Was driving me crazy for a while. Anyone experience side-effects of removing system-polyfills.js, though?

@vsavkin
Copy link
Contributor

vsavkin commented Oct 4, 2016

It seems the issue was opened before the router rewrite. And it doesn't look like the issue still makes sense. I'm closing it. Please reopen it if you think it still makes sense.

@vsavkin vsavkin closed this as completed Oct 4, 2016
@hari30OJ
Copy link

@azjkjensen Can you share your solution if you found one. I am using webpack for building.
Any help would be appreciated. Thanks!!

@after-ephemera
Copy link

@hari30OJ unfortunately I'm no longer a part of the project and I can't remember what the fix was. I'm sorry.

@Velunce
Copy link

Velunce commented Jul 8, 2018

Cut it to another folder and then cut back to src folder......
It will works.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests