如何将操作从父视图/组件传递到子组件,并仍然维护父组件的上下文.下面的plunkr中显示的问题表明,如果操作在组件中执行,则它位于组件的上下文中,而不是传递操作的父级.基本上是典型的"那个=这个"问题.
是的,您可以使用eventAggregator来执行此操作,但如果没有它,您会怎么做?你需要将整个viewModel传递给组件吗?
http://plnkr.co/edit/n1xPUdR5nhXwwIivawBj?p=preview
// app.js
import { inject } from 'aurelia-framework';
import { MyService } from './my-service';
@inject(MyService)
export class App {
constructor(myService) {
this.myService = myService;
this.message = "Hello World!";
}
doThing() {
console.log('do a thing');
this.myService.foo();
}
}
<!--app.html-->
<template>
<require from="./my-component"></require>
<p>${message}</p>
<button click.delegate="doThing()">Do the thing - in app.html</button>
<my-component do.bind="doThing"></my-component>
</template>
// my-component.js
import { bindable } from 'aurelia-framework';
@bindable('do')
export class MyComponentCustomElement {
}
<!-- my-component.html -->
<template>
<button click.delegate="do()">Do the thing - in my-component</button> …Run Code Online (Sandbox Code Playgroud)