[英]router.navigateByUrl() causes navigation to root and reload, angular 2.4.7
When I use router.navigate() or navigateByUrl(), I get a reload - a call to the root. 当我使用router.navigate()或navigationByUrl()时,我会重新加载-调用根目录。 The Chrome Dev tools console shows
Navigated to http://localhost/
Chrome开发者工具控制台显示
Navigated to http://localhost/
Navigation via links with <a [routerLink]=
works correctly. 通过
<a [routerLink]=
链接进行的导航正常工作。
this.router.navigateByUrl('/test1/test11');
this.router.navigateByUrl('/test1/test11');
angular-cli: 1.0.0-beta.28.3 node: 7.5.0 os: win32 x64 @angular/common: 2.4.7 @angular/compiler: 2.4.7 @angular/core: 2.4.7 @angular/forms: 2.4.7 @angular/http: 2.4.7 @angular/platform-browser: 2.4.7 @angular/platform-browser-dynamic: 2.4.7 @angular/router: 3.4.7 @angular/compiler-cli: 2.4.7
Is published to GitHub, published to GitHub https://github.com/michaelkariv/angular_router_demo
发布到GitHub, 发布到GitHub
https://github.com/michaelkariv/angular_router_demo
serve --host localhost --port 80 --live-reload false -prod
As per your code, when anchor clicked it will trigger browser's default navigation 根据您的代码,单击锚点时将触发浏览器的默认导航
<a href="" (click)="goToTest11()"> router.navigateByUrl('../test1/test11')</a>
to fix that use event.preventDefault() 修复使用event.preventDefault()
<a href="" (click)="$event.preventDefault();goToTest11();"> router.navigateByUrl('../test1/test11')</a>
I haven't seen your code so this is just a guess, but I think you have missed router-outlet in your app.component.html 我还没有看到您的代码,所以这只是一个猜测,但是我认为您在app.component.html中错过了路由器插座。
<div>
<router-outlet></router-outlet>
</div>
Add the above code to your app.component.html file in the end 最后将上述代码添加到您的app.component.html文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.