admin管理员组文章数量:1173665
I have worked with routerLink and it works very well :
<a routerLink="inscription" class="btn btn-default">Inscription</a>
<a routerLink="login" class="btn btn-default">Se connecter</a>
I am trying now to deal with routerLink
inside the typescript file. Exactly, when i click on a button, i call a function inside this function i want to call routerLink
how could i do it? and if is calling it will reload the page or it will work exactly the same as the routerLink
above ?
I have worked with routerLink and it works very well :
<a routerLink="inscription" class="btn btn-default">Inscription</a>
<a routerLink="login" class="btn btn-default">Se connecter</a>
I am trying now to deal with routerLink
inside the typescript file. Exactly, when i click on a button, i call a function inside this function i want to call routerLink
how could i do it? and if is calling it will reload the page or it will work exactly the same as the routerLink
above ?
4 Answers
Reset to default 42In code, you use .navigate instead:
this.router.navigate(['/products']);
You pass it the same parameter as you do the routerLink
.
try this
<a javascript:void(0) routerLink="inscription" class="btn btn-default">Inscription</a>
Please note that though the URL in the browser is not case sensitive, Router.navigate is.
Correct: this.router.navigate(['/products']);
.
Incorrect: this.router.navigate(['/Products']);
From what I have learned, routerLink is simply a wrapper for the Router method navigateByUrl(...)
. I was using routerLink so that I can update a secondary outlet. This worked for me:
this.router.navigateByUrl(this.router.createUrlTree(["/<parent>", {outlets: {'<outlet_name>': ['<outlet_path>']}}]))
To describe in words, router.navigateByUrl
seems to prevent the page reload, but it only accepts a pure url, no params (to my understanding).
I use router.createUrlTree(...)
to generate a "pure" url from the additional outlet/query/etc params I need the url to have. Maybe it would read better like this:
generatedUrl = this.router.createUrlTree(["/<parent>", {outlets: {'<outlet_name>': ['<outlet_path>']}}])
router.navigateByUrl(generatedUrl)
本文标签: javascriptAngular4routerLink with typescriptStack Overflow
版权声明:本文标题:javascript - Angular4 - routerLink with typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737584261a1997626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论