ember replaceWith不会更改网址

问题描述 投票:0回答:1

如何在相同的路径上使用不同的参数执行某些replaceWith

路线声明为:

this.route('sampleRoute', { path: '/:param_name' });

此“ sampleRoute”路由重现了问题:URL内的replaceWith之后,URL不变。

let globalFlag = null;

export default Route.extend({
  afterModel() {

    console.log("welcome");

    if(globalFlag){
      console.log(this.router.location.getURL());
      console.log(this.paramsFor(this.routeName).param_name);
    } else {
      globalFlag = true;
      this.replaceWith(this.routeName, 'progValue');
    }
  }
});

尝试过beforeModel,model和afterModel。在运行一些代码之前如何正确设置URL?

使用http://localhost/sampleRoute/browserValue测试此路由会产生:

Expected output: 
    welcome
    welcome
    /sampleRoute/progValue
    progValue

Actual output:
    welcome
    welcome
    /sampleRoute/browserValue
    progValue
ember.js ember-router
1个回答
0
投票

If /sampleRoute/之后的URL段不是动态的,我认为您正在寻找的是嵌套路由而不是查询参数。您的router.js文件将具有:

this.route('sampleRoute', function() {
  this.route('someNestedRoute');
});

或者]如果段是动态的,则您的router.js文件为:

this.route('sampleRoute');

重要的是,您不需要将参数指定为路线的path选项。

在您的模型中,replaceWith为查询参数使用哈希:

import { set } from '@ember/object';

export default Route.extend({
  globalFlag: null,

  beforeModel(transition) {
    super.beforeModel(transition);

    if (this.globalFlag){
      console.log('flag set');
    } else {
      set(this, 'globalFlag', true);
      this.replaceWith(this.routeName, transition.to.queryParams);
    }
  }
});

但是

我想问你在这里想做什么。您应该能够让余烬的路线和控制器为您管理查询参数,并且根本不需要这样replaceWith。我会查看指南:https://guides.emberjs.com/release/routing/query-params/
© www.soinside.com 2019 - 2024. All rights reserved.