2017-08-18 35 views
0

在Aurelia中,当我想访问作为aurelia自定义元素的DOM元素的视图模型时,我可以使用Aurelia重视的au属性,如componentElement.au.controller.viewModel获取无容器元素的ViewModel

当我的自定义元素是无容器的(属性级别为属性@containerless)时,属性au不可用。

这个要点说明了这一点: https://gist.run/?id=928f97f49c01c1db10d8bf4399f5c335

如何访问一个无容器自定义组件的视图模型时,我只需要它的DOM元素的引用?

回答

0

达到你想要挂接到created生命周期在您的视图模型是什么:

class ViewModel { 
    created(owningView, view) { 
    view.controller 
    view.controller.viewModel // <-- this is what you want 
    } 
} 
+0

如果'不太comp'这样做,那么'view.controller'是'undefined'。我猜是因为它是无容器组件。另外'view.controller.viewModel'会等于'this',这有什么意义? – ZoolWay

+0

没有仔细阅读问题,检查了您的使用情况,可以看到您想要的内容不会被'@ children'支持。也许有消息 – bigopon

1

我不知道这是否是你想要的,但你可以使用view-model.ref。例如:

<less-comp text="item three, containerless" view-model.ref="test"></less-comp> 

用法:

export class App { 
    attached() { 
    console.log(this.test); 
    } 
} 
+0

这种方式'less-comp'虚拟机在应用程序中可用,但它应该在'list-comp'(我当前输出那些控制台日志)中。如果我用'repeat.for'编写'less-comp',这个工作又会如何呢? – ZoolWay