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

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

问题描述

我想更新地址栏中的url参数而不进行路由. 但是我不确定如何从视图模型中使用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

然后,我希望能够在不重新激活视图模型的情况下从网址中删除ID ,网址结果示例: 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.

Params

  • 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:使用Router从VM修改路由参数和地址栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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