简体   繁体   English

如何在没有主题的情况下将数据从服务传递到组件?

[英]How to pass data from Service to Component without Subject?

Below are 2 components first and second and it has a service to pass data. 以下是第一个和第二个2个组件,它具有传递数据的服务。 2 components have the same form since its an exam how to type at first comp and display what is typed at second comp. 2个组件具有相同的形式,因为它检查了如何在第一个小节中键入和显示在第二个小节中键入的内容。 So form is built at service and it's initialized at each component. 因此,表单是在服务时构建的,并在每个组件处进行了初始化。

I am able to get and pass data from service and want to pass data from service to second comp without using Subject. 我能够从服务获取和传递数据,并希望在不使用Subject的情况下将数据从服务传递给第二个comp。

----first---
constructor(private service: TestServiceService) 
{ }

ngOnInit() {
    const sfm = new InputForm();
    this.form = this.service.createform(sfm);
    this.passDataInput();
}

passDataInput() {
     this.form.valueChanges.subscribe(val => {
         this.service.getData(val);
     });

-----service----
constructor(private formBuilder: RxFormBuilder)
{ }

getData(datalist: TipoData) {
    console.log('service ', datalist);
}

---second-----
ngOnInit() {
     const sfm = new InputForm();
     this.form = this.service.createform(sfm);
     this.form.disable();
     this.service.getData(this.data); 
}

Let me explain two ways may be helpful to you. 让我解释两种方式可能对您有帮助。 considering you strictly don't want to use Subject. 考虑到您严格不想使用Subject。

1) if your first component and second component both shares the same html you can use the EventEmitter approach. 1)如果您的第一个组件和第二个组件都共享相同的html,则可以使用EventEmitter方法。

2) if they are on different route you can subscribe to params and on response you can ask your service to give you data which you already have it. 2)如果他们在不同的路线上,则可以订阅params,并在响应时可以要求服务向您提供已经拥有的数据。


Update 更新资料

Here I'm giving you answer for my option 1 where I assume you have both component shares same html. 在这里,我为您提供我的选项1的答案,假设您的两个组件共享相同的html。

I have created two component named first and second and they shares same html app.component.html 我创建了两个名为firstsecond的组件,它们共享相同的html app.component.html

Brief View: 简要视图:

Here first component has form with one input, When submitted we pass the formData by eventEmitter. 在这里,第一个组件具有带一个输入的表单,提交后,我们通过eventEmitter传递formData。 We capture the data on parent on upon getting the data we toggle second component to display received data. 我们在获取父数据时捕获数据,然后切换第二个组件以显示接收到的数据。

here how I code it with very minimal approach. 在这里,我如何用最少的方法编写代码。

app.component.html app.component.html


    <div>
      <app-first (onSubmitFormData)="updateFormData($event)"></app-first>
      <app-second formData="{{dataReceived}}" *ngIf="isVisible"></app-second>
    </div>


app.component.ts app.component.ts



     dataReceived;
      isVisible = false;

      updateFormData(data) {
        this.isVisible = true;
        this.dataReceived = data;
      }

first.component.html first.component.html


    <form (ngSubmit)="onSubmit()" #f="ngForm">
      <input type="text" name="username" ngModel />
      <button type="submit">Submit</button>
    </form>

first.component.ts first.component.ts


      @ViewChild('f') form: NgForm;
      @Output() onSubmitFormData = new EventEmitter<any>();

      constructor() { }
      onSubmit() {
        this.onSubmitFormData.emit(JSON.stringify(this.form.value));
      }

second.component.html second.component.html

<p> {{ secondCompoData.username }} </p>

secon.component.ts secon.component.ts



    @Input() formData;
      secondCompoData;
      constructor() { }

      ngOnInit() {
        this.secondCompoData = JSON.parse(this.formData);
      }

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

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