router.navigate更改URL,但未呈现组件 [英] router.navigate changes the URL, but is not rendering the component
问题描述
我不想在模板中使用<a href="/my-path/{{my_param}}"></a>
,而是愿意使用带有参数的函数将我重定向到页面.
Instead of using <a href="/my-path/{{my_param}}"></a>
in my template, I am willing to use a function with paramters to redirect me to a page.
因此,这就是我在.ts文件中构建函数的方式:
So, this is how I built my function in my .ts file:
redirectToChat(my_param){
this.router.navigate(['./my-path/' + my_param ]);
}
以及我在模板中如何调用它:
And how I called it in my template:
<div (click)="redirectToChat(my_param)">
...
</div>
它更改URL,但不呈现组件. 我将此导入了我的组件:
It changes the URL, but not rendering the component. I imported this in my component:
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
我在构造函数中使用了它:private router: Router
And I used it in my constructor: private router: Router
您可以帮助我正确渲染组件吗?我该如何使用功能将用户重定向到页面?
Can you help me to render corrctly the component? How else can I redirect the user to the page using a function?
推荐答案
您正在将导航(到组件)"与按网址导航"混合使用. 您是否尝试过navigationByUrl?
You are mixing navigate (to component) with navigate by url. Have you tried navigateByUrl?
this.router.navigateByUrl('./my-path/' + my_param);
这篇关于router.navigate更改URL,但未呈现组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!