【发布时间】:2018-10-09 10:42:16
【问题描述】:
最近,我正在尝试 Angular5,我想知道在其他组件中触发事件的最佳实践。
这就是我现在的应用程序结构:
index.html
<main-app></main-app>
app.component.html
<main-navbar></main-navbar>
<router-outlet></router-outlet>
<auth-modal></auth-modal>
身份验证模态组件
import { Component, EventEmitter } from '@angular/core';
@Component({
selector: 'auth-modal',
templateUrl: 'modal.component.html'
})
export class AuthModal {
hidden: boolean = true;
closed: EventEmitter<any> = new EventEmitter();
close(event: KeyboardEvent){
this.hidden = !this.hidden;
this.closed.next(true);
event.stopPropagation();
}
}
所以 main-navbar 组件基本上是 html,里面没有逻辑。我想要解决的是,构建自定义模式(如引导程序),所以当我点击导航栏中的按钮时,它会触发 modal 组件并打开。
我希望我应该遵循哪种方法,而不是代码。
我已经搜索过这个,但我不喜欢使用第三方包,因为主要目的是学习而不是达到预期的结果。
解决方案
感谢https://stackoverflow.com/users/6036154/embarq 和https://stackoverflow.com/users/271012/ryan 我在这里得到了解决方案: https://angular-jswxqi.stackblitz.io/
【问题讨论】:
-
考虑接受答案。
标签: javascript angular angular5