如何在相同的路径上使用不同的参数执行某些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
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/