简体   繁体   中英

script is not working with routerLink in Angular 2/4

I'm using Materialize CSS in my Angular app and the scrip initialization doesn't work with routerLink

<a routerLink="" />

It's just working when i use a href link - normal link.

<a href="" />

How can i resolve that ?

What from Materialize doesn't work? You just mention scrip initialization , but I'm not sure what you mean.

I will add though that the use of href will essentially refresh the "refresh" the page as it will request resources from the server, where as the routerLink will only tell the Angular app to navigate inside of the application. I'm assuming the issue comes from the fact that when navigating to a new route Materialize has already ran its code, and the newly created components do not have the JQuery applied to them.

With that in mind I would advise you looking at the AfterViewInit lifecycle hook as you will need to manually initialize the JQuery plugins. In fact a lot of the JavaScript driven bits on the Materialize documentation have a jQuery Plugin Initialization section where you can put that code into your AfterViewInit lifecycle hook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM