简体   繁体   English

如何将数据或警报消息从服务传递回组件

[英]How to pass data or alert messages from service back to component

I am using Angular 6 and I have the upload file control on 3 different screens ( 3 different component), all of them calling the same method UploadFile() but issue is whenever i have to make any changes to this method, I have to update this method in all 3 places. 我正在使用Angular 6,我在3个不同的屏幕(3个不同的组件)上有上传文件控件,所有这些都调用相同的方法UploadFile()但问题是每当我必须对此方法进行任何更改时,我必须更新这个方法在所有3个地方。 So, I thought to create a service (non-shared) with this method UploadFile() so that I have to make changes at one place and to be called by all 3 screens. 所以,我想用这个方法UploadFile()创建一个服务(非共享),这样我就必须在一个地方进行更改并被所有3个屏幕调用。 I have a challenge that how to return the alert messages and how to return both success, failure response from the another service inside this service back to the components. 我有一个挑战,如何返回警报消息以及如何将此服务中的另一个服务的成功,失败响应返回给组件。


@Injectable({
  providedIn: 'root'
})

export class MyFileUploader {

  fileuploadList: EventUpload[];
  eventId: Number;
  constructor(private dateFormatHelper: DateFormatHelper, private manageService: ManageService) {
  }

public UploadFile(event) {
     const fileList: FileList = event.target.files;
     if (fileList.length > 0) {
         const file: File = fileList[0];
         const extension = fileList[0].name.split('.').pop();

        if (this.fileuploadList.find( f => f.fileName === file.name)) {
           alert('Duplicate file identified!');
           return;
        }
         const formData: FormData = new FormData();
         const params = Object.assign({}, {
              Id: 0,
             F ileName: file.name,
              FilePath: 'XXXXXX', 

         });
         formData.append('uploadFile', file, params.FileName);
         formData.append('data', JSON.stringify(params));
         this.manageService.uploadAttachments(formData).subscribe (resp => {
           if (resp === -1 ) {
             alert('Error occured while uploading the attachment');
             return;
            } else {
              this.fileuploadList.push (new DataUpload (0, params.FileName, 
           params.CreatedBy,));
             }
         },
       (error) => {
         console.log('POST ERROR in method uploadAttachments: ' + error.error);
       }
       );
     }
 }
}

Your UploadFile method can return a Subject. 您的UploadFile方法可以返回主题。 Something like this: 像这样的东西:

public UploadFile(event) {
 const uploadSubject: Subject<any> = new Subject();
 const fileList: FileList = event.target.files;

Import like this: 导入如下:

import { Subject } from 'rxjs';

And use it like this: 并像这样使用它:

    if (this.fileuploadList.find( f => f.fileName === file.name)) {
       alert('Duplicate file identified!');
       uploadSubject.next('Duplicate');
       uploadSubject.complete();
    }

On the caller side: 在来电方面:

this.myFileUploaderService.UploadFile(event)
 .subscribe(
   (response) => console.log(response), 
   (error) => console.error(error),
   () => console.log('completed')
)

Every time you call UploadFile method you can subscribe to the Subject to receive informations from the Service. 每次调用UploadFile方法时,您都可以订阅Subject以从服务接收信息。

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

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