我在我的父组件中,我想检测发出的事件。
child.component.ts
@Component({
selector: 'child-component',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {
@Input() public state: String = 'CLOSED';
@Output() public stateChanged: EventEmitter<String> = new EventEmitter<String>();
this.stateChanged.emit(this.state);
}
父组件.html
<router-outlet (stateChanged)="onStateChange($event)"><router-outlet>
父组件.ts
onStateChange(event){
console.log(event);
}
这对我不起作用!
编译后的 HTML 代码如下所示:
<router-outlet></router-outlet>
<child-component>
// all child component code
<child-component>
有没有办法推送
(stateChanged)="onStateChange($event)"
在 <child-component>
元素?有人可以帮忙吗?
最佳答案
<router-outlet></router-outlet>
被认为是 placeholder
目的是添加 routed components
.它不支持任何类型的绑定(bind)。
但是有更新到 <router-outlet></router-outlet>
带有允许添加组件的事件:Source
<router-outlet (activate)="componentAdded($event)" (deactivate)="componentRemoved($event)"></router-outlet>
这有助于通过
componentAdded()
与 getter、setter 和方法进行通信首选方法是使用共享服务检查这些链接以创建共享服务
1) Angular Doc
2) Create shared service angular
3) Example1 Example2
关于angular - eventEmitter 可以在路由器 socket 上工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51372269/