【发布时间】:2018-12-24 14:49:40
【问题描述】:
我在我的父组件中,我想检测发出的事件。
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);
}
parent.component.html
<router-outlet (stateChanged)="onStateChange($event)"><router-outlet>
parent.component.ts
onStateChange(event){
console.log(event);
}
这对我不起作用!
编译后的 HTML 代码如下:
<router-outlet></router-outlet>
<child-component>
// all child component code
<child-component>
有没有办法将(stateChanged)="onStateChange($event)" 推入<child-component> 元素中?
有人可以帮忙吗?
【问题讨论】:
-
使用服务。路由使您的组件与渲染层次结构分离,不要试图改变它
-
如果可能的话,您能举个例子吗?
-
@Injectable({providedIn: 'root') export default class {change = new Subject(); triggerChange() { this.change.next();}
标签: angular eventemitter router-outlet