Aurelia-Router:使用路由器从 VM 修改路由参数和地址栏 [英] Aurelia-Router: Modify route params and address bar from VM with Router

查看:24
本文介绍了Aurelia-Router:使用路由器从 VM 修改路由参数和地址栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want update the url-params in the address bar without routing. But i'm not sure how to do this with Aurelia-router from a view-model.

在我的例子中,我在 url 中发送 ID,这些 ID 被视图模型的 activate-method 获取.

In my case I send IDs in the url which gets picked up by the view-model's activate-method.

路线如下:http://localhost:3000/#/test/products?0=2599037842&1=2599080552

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

Then I want to be able to remove IDs from the url without reactivating the view-model, url result exemple: http://localhost:3000/#/test/products?0=2599037842

希望 Aurelia-router 支持此功能

Hopefully there is support for this in Aurelia-router

谢谢!/迈克

推荐答案

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

Yes, you can do that with router.navigateToRoute() method. navigateToRoute has additional parameters. Use options (third) parameter to modify how the navigation is done.

示例:

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
        );
    }
}

来自文档中心:

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

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

Navigates to a new location corresponding to the route and params specified.

参数

  • route: string - 生成导航位置时使用的路线名称.
  • params?: any - 填充路由模式时要使用的路由参数.
  • options?: any - 导航选项.
  • route: string - The name of the route to use when generating the navigation location.
  • params?: any - The route parameters to be used when populating the route pattern.
  • options?: any - The navigation options.

通过options,您可以控制历史记录已更新.

  • trigger: false - 防止路由器导航管道被触发
  • replace: true - 用提供的路由替换历史中的当前 URL(重写历史),所以它不会被浏览器返回功能触发
  • trigger: false - prevents the router navigation pipeline to be triggered
  • replace: true - replaces the current URL in history with provided route (rewriting history), so it won't be triggered with browser back functionality

这篇关于Aurelia-Router:使用路由器从 VM 修改路由参数和地址栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆