首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跨所有部件的角度共享模态

跨所有部件的角度共享模态
EN

Stack Overflow用户
提问于 2018-07-05 21:44:18
回答 1查看 2.8K关注 0票数 3

我正在尝试实现自定义确认对话框功能,这是计划在所有组件的所有可用的应用程序。

为此,我使用Angular Material

模态是一个单独的组件,我以下列方式在另一个组件中解析它:

代码语言:javascript
复制
const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, ... });

我所面临的问题----使用这种方法,我必须在每个组件中复制代码。违反了DRY principle

欲知更多详情:

代码语言:javascript
复制
...
export class SearchComponent extends AppComponentBase {...

    constructor(public confirmDialog: MatDialog, ...) { super(injector); }

    confirm(title: string, message: string) {
        var promise = new Promise((resolve, reject) => {

            const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, {
                width: '250px',
                data: { title: title, message: message }
            });

            dialogRef.afterClosed().subscribe(result => {
                if (result) {
                    resolve();
                } else {
                    reject();
                }
            });
        });
        return promise;
    }

显然,我可以将共享代码移动到一个基本组件-- AppComponentBase。尽管仍然会有一些重复的代码,例如与构造函数相关的代码。

但是,在软件设计方面是否有更好/更干净的方法来重构我所拥有的呢?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-05 22:28:06

对于一个工作示例,StackBlitz

将其放在根级别的/shared或/services文件夹中。

服务:

代码语言:javascript
复制
import { Observable } from 'rxjs';
import { MessagesComponent } from './messages.component';
import { MatDialogRef, MatDialog } from '@angular/material';
import { Injectable } from '@angular/core';

@Injectable()
export class MessagesService {

  dialogRef: MatDialogRef<MessagesComponent>;

  constructor(private dialog: MatDialog) { }

  public openDialog(title: string, message: string): Observable<any> {
    this.dialogRef = this.dialog.open(MessagesComponent);
    this.dialogRef.componentInstance.title = title;
    this.dialogRef.componentInstance.message = message;

    return this.dialogRef.afterClosed();

    // Nothing can live after afterClosed.
  }
}

The component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';

import { MatDialogRef } from '@angular/material';


@Component({
  selector: 'app-messages',
  templateUrl: './messages.component.html'
})
export class MessagesComponent implements OnInit {

  public title: string;
  public message: string;


  constructor(
    private dialogRef: MatDialogRef<MessagesComponent>,
  ) { }


  private closeWithTimer() {
    setTimeout (() => {
      this.dialogRef.close();
    }, 2000);
  }


  ngOnInit() {
    this.closeWithTimer();
  }
}

html:

代码语言:javascript
复制
<h1 mat-dialog-title>{{title}}!</h1>
<div mat-dialog-content>{{message}}</div>

从你的宇宙中的某个组件呼叫:

代码语言:javascript
复制
constructor(
    private httpService: HttpService,
    public dialogRef: MatDialogRef<AddMemberComponent>,  // Used by the html component.
    private messagesService: MessagesService,
    public formErrorsService: FormErrorsService
  ) { }

this.httpService.addRecord(this.membersUrl, enteredData)
      .subscribe(
        res => {
          this.success();
        },
        (err: HttpErrorResponse) => {
          console.log(err.error);
          console.log(err.message);
          this.handleError(err);
        }
      );

在该组件的ts底部:

代码语言:javascript
复制
  private success() {
    this.messagesService.openDialog('Success', 'Database updated as you wished!');
  }

  private handleError(error) {
    this.messagesService.openDialog('Error addm1', 'Please check your Internet connection.');
  }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51200285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档