角度刷新组件

Dev*_*per 0 angular

我在顶部导航栏中放下位置。在更改位置时,我必须根据该位置数据加载整个应用程序,因此我需要刷新所有组件数据,这就是为什么我要执行此步骤。

core.js:5847错误错误:未被捕获(承诺):错误:无法匹配任何路由。URL段:'RefreshComponent'错误:无法匹配任何路由。网址段:“ RefreshComponent”

constructor(
private router: Router,
) { }
Run Code Online (Sandbox Code Playgroud)

我用过javascript,setTimeout(() => { location.reload(); });它工作正常,我不想重新加载页面,只想刷新我在下面的代码中尝试过的组件。但是当我使用控制台错误来了。

changeLocation(locationData) {
    this.router.navigateByUrl('/RefreshComponent', { skipLocationChange: true }).then(() => {
        this.router.navigate([this.router.url]);
    }); 
}
Run Code Online (Sandbox Code Playgroud)

我是否缺少任何配置。?

Plo*_*hie 5

您可能RefreshComponent在路由配置中没有路由。

至于刷新组件,只需按如下所示修改函数,就不需要RefreshComponent路由了。

替换navigateByUrl('/RefreshComponent',...navigateByUrl('/',...

changeLocation(locationData) {

    // save current route first
    const currentRoute = this.router.url;

    this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => {
        this.router.navigate([currentRoute]); // navigate to same route
    }); 
}
Run Code Online (Sandbox Code Playgroud)