您應該嘗試以另一種方式解決此問題。爲什麼你要導航到ViewModel並觸發它的創建,只是爲了不使用它並加載另一個ViewModel?似乎效率最低的權利?
Aurelia暴露路由器上的管道,你應該檢查那裏並相應地重定向。看看預活化步驟here,你可以寫這樣的(僞代碼):
configureRouter(config, router) {
function step() {
return step.run;
}
step.run = async (navigationInstruction, next) => {
if(await this.service.RecordExists(navigationInstruction.queryParams...)
{
return next()
} else {
next.cancel(new Redirect('your other page'))
}
};
config.addPreActivateStep(step)
config.map([
{ route: ['', 'home'], name: 'home', moduleId: 'home/index' },
{ route: 'users', name: 'users', moduleId: 'users/index', nav: true },
{ route: 'users/:id/detail', name: 'userDetail', moduleId: 'users/detail' },
{ route: 'files/*path', name: 'files', moduleId: 'files/index', href:'#files', nav: true }
]);
}
編輯
您可以在您不想重定向的情況下,比如你有一個用戶想要書籤的BaseURL/BusinessObject的/ ID和URL是可導航前的物體實際存在 然後你可以使用getViewStrategy()
功能上的視圖模型:
getViewStrategy(){
if(this.businessObj){
return 'existingObjectView.html';
} else {
return 'nonExisting.html';
}
}
謝謝回答,看看你的代碼。似乎是一個重定向正在發生,因此改變url正是我想避免 next.cancel(新的重定向('你的其他頁面'))。 – crypted
你能解釋爲什麼你想避免重定向嗎?您可以使用相同的viewmodel輕鬆使用2個視圖,只需爲其提供2個路由並設置即可。您可以使用'''compose'''元素來解決您的問題,但它會增加一些複雜性,並且我看不到您的案例 –
中增加的值,因爲用戶應該能夠爲此頁添加書籤,並且在將來此頁面可能可用。 – crypted