Aurelia-Router:使用Router修改VM的路由参数和地址栏

Mik*_*ike 9 aurelia aurelia-router

我想在没有路由的情况下更新地址栏中的url-params.但我不知道如何使用Aurelia-router从视图模型中做到这一点.

在我的情况下,我在URL中发送ID,由视图模型的activate-method拾取.

这条路线是这样的: HTTP://本地主机:3000 /#/测试/产品0 = 2599037842&1 = 2599080552

然后我希望能够从URL中删除ID 而不重新激活视图模型,url结果例如: http:// localhost:3000 /#/ test/products?0 = 2599037842

希望在Aurelia-router中有这方面的支持

谢谢!/麦克风

Mir*_*vic 21

是的,你可以用router.navigateToRoute()方法做到这一点.navigateToRoute有额外的参数.使用options(第三个)参数修改导航的完成方式.

例:

import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';

@inject(Router)
export class Products {
    constructor(router) {
        this.router = router;
    }

    activate(params) {
        // TODO: Check your params here and do navigate according to values

        this.router.navigateToRoute(
            this.router.currentInstruction.config.name, // current route name
            { '0': params['0'] }, // route parameters object
            { trigger: false, replace: true } // options
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

文档中心:

navigateToRoute(route: string, params?: any, options?: any): boolean

导航到与指定的路径和参数对应的新位置.

PARAMS

  • route: string - 生成导航位置时要使用的路径的名称.
  • params?: any - 填充路径模式时要使用的路径参数.
  • options?: any - 导航选项.

options您控制历史记录的更新方式.

  • trigger: false - 防止触发路由器导航管道
  • replace: true - 使用提供的路径(重写历史记录)替换历史记录中的当前URL,因此不会使用浏览器后退功能触发它