【问题标题】:Angular 6 route params reload after Location.goLocation.go 后重新加载 Angular 6 路由参数
【发布时间】:2019-02-12 11:13:21
【问题描述】:

为了避免重新加载任何组件,我使用 Location.go 更改我的第一个组件 (SearchboxComponent) 的 URL。

问题是我在第二个组件 (ListingComponent) 中使用路由参数来反映某些链接、分页等中的 URL 更改。但是我的订阅未检测到新 URL并且参数不会改变。我可以让路由器模块重新解析 URL 以填充新参数吗?

列表组件

this.route.params.subscribe( async (params: IListingUrlParams) => {
    console.log('params', params);
});

搜索框组件

const url = this.parametersService.searchParamsToUrl(params);
// this.router.navigate([url]); This works but reloads my main component
this.listingsService.getList(params); // Fetch the new data before navigation
Location.go(url); // This works but params are not reloaded

【问题讨论】:

    标签: javascript angular typescript angular6


    【解决方案1】:

    如果您想更新您当前的路线参数。

    假设您的路线为:

    路径/:id

    当前路径:path/123

    在组件中:

    import { Location } from '@angular/common';
    
    constructor(private location: Location) { }
        updateRoute(){
        let url = this.location.path().replace('123', '456');
          this.location.go(url);
        }
    

    更新路径:path/456

    【讨论】:

    • 所以我需要手动重新加载每个参数,而不是重新解析整个 URL?
    • 您也可以匹配竞争网址。我刚刚做了一个参数替换。,
    • 它只推荐用于参数而不是路由。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2016-06-07
    • 2018-03-20
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多