简体   繁体   English

从组件触发模态事件的最佳实践

[英]best practice to trigger modal event from component

Recently, I am trying Angular5 and I would like to know the best practice for triggering events in other component. 最近,我正在尝试Angular5,我想知道触发其他组件中事件的最佳实践。

So this is my application structure right now: 所以这是我现在的应用程序结构:

index.html 的index.html

<main-app></main-app>

app.component.html app.component.html

<main-navbar></main-navbar>
<router-outlet></router-outlet>

<auth-modal></auth-modal>   

auth modal component 身份验证模式组件

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();
    }
}

So main-navbar component is basically html, no logic inside. 所以main-navbar组件基本上是html,内部没有逻辑。 What I am trying to approach is, building custom modal (like bootstrap), so when I click a button in navbar it triggers modal component and it opens. 我要尝试的方法是构建自定义模式(如引导程序),因此当我单击导航栏中的按钮时,它将触发模式组件并打开。

Rather than code, I would like which approach should I follow. 我想使用哪种方法而不是代码。

I have searched about this, but I do not like to use third party packages, since the main purpose is to learn instead of achieving desired result. 我已经搜索过此内容,但是我不喜欢使用第三方软件包,因为主要目的是学习而不是获得期望的结果。

SOLUTION

Thanks to https://stackoverflow.com/users/6036154/embarq and https://stackoverflow.com/users/271012/ryan I got the solution here : https://angular-jswxqi.stackblitz.io/ 感谢https://stackoverflow.com/users/6036154/embarqhttps://stackoverflow.com/users/271012/ryan,我在这里找到了解决方案: https : //angular-jswxqi.stackblitz.io/

I propose you to implement service for controlling the modal. 我建议您实施用于控制模式的服务。 Prototype for this service could be 该服务的原型可能是

enum ModalState { Close, Open }


class ModalController {
    public readonly modalState$: Subject<ModalState>;

    constructor() {
        this.modalState$ = new Subject<ModalState>();
    }

    public open() {
        this.modalState$.next(ModalState.Open);
    }

    public close() {
        this.modalState$.next(ModalState.Close);
    }
}

So this minimal implementation can be used as follows: 因此,此最小实现可以按如下方式使用:

  1. ModalComponent subscribes to ModalController.modalState$ and handles changes ModalComponent订阅ModalController.modalState$并处理更改
  2. ModalService.open and ModalService.close can be used by any components in the whole app 整个应用程序中的任何组件都可以使用ModalService.openModalService.close

One approach is to use a Service that emits events and that can be subscribed to also. 一种方法是使用发出事件并且也可以订阅的服务。 Provide the service in AppModule, and inject it into the modal, which listens for the event, and into any component that might trigger the modal to display. 在AppModule中提供服务,然后将其注入侦听事件的模式中,并注入可能触发模式显示的任何组件中。

If you want there is also the Angular Material CDK (component development kit), which has a Portal and PortalHost - purposely built for injecting things like modals into the DOM 如果需要,还可以使用Angular Material CDK(组件开发套件),它具有Portal和PortalHost-专门用于将诸如模式之类的内容注入DOM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM