首页 > 解决方案 > 角度刷新组件

问题描述

我在顶部导航栏中有位置下拉菜单。在更改位置时,我必须根据该位置数据加载整个应用程序,因此我需要刷新所有组件数据,这就是我接近这一步的原因。

core.js:5847 错误错误:未捕获(承诺):错误:无法匹配任何路由。URL 段:“RefreshComponent”错误:无法匹配任何路由。URL 段:“刷新组件”

constructor(
private router: Router,
) { }

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

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

我缺少任何配置吗?

标签: angular

解决方案


您的路线配置中可能没有RefreshComponent路线。

至于 refresing 你的组件,只需如下修改你的函数,你不需要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
    }); 
}

推荐阅读