在我们的项目中,我们有一些自定义元素,像这样:
<entity-link id="entity.id>
基本上它只是呈现一个链接来编辑实体屏幕
<template>
<a class="entity-link"
route-href="route: ENTITY_EDIT; params.bind: { id: entity.id }"
>${entity.name}
</a>
</template>
问题是这在Aurelia Dialog上下文中根本不起作用。href
属性根本没有填充。
我试图调查这个问题,我把路由器直接注入到对话框的视图模型
import {Router} from 'aurelia-router';
@inject(DialogController, Router)
export default class RecordDetailsDialog {
constructor(dialogController:DialogController, router:Router) {
this.controller = dialogController;
this.router = router; /// WRONG INSTANCE!!!
}
}
,发现注入了错误的Router实例。主路由器(approouter)没有定义ENTITY_EDIT路由,它是在子路由configureRoute函数中动态添加的。
我不明白为什么注入的路由器是主路由器,而不是传递给启动对话框打开的视图的路由器。
有任何建议请参考
所以在阅读aurelia的源代码2小时后,我发现DialogService实例是在根DI容器中创建的,该容器与根路由器相关联,而根路由器不知道子路由。我通过在子视图模型容器
中手动注册DialogService实例来解决问题。 import {Container} from 'aurelia-dependency-injection';
import {CompositionEngine} from 'aurelia-templating';
import {DialogService} from 'aurelia-dialog';
export class Main {
constructor(container:Container, compositionEngine:CompositionEngine){
container.registerInstance(DialogService, new DialogService(container, compositionEngine))`
}
...
}
但是感觉很粗糙,仍然在想是否有更干净的方法来解决这个问题。