Angular8 Subject 同级组件通讯

Angular8 Subject 同级组件通讯

1.文件结构

在这里插入图片描述

2.数据结构

models/student-info.model.ts

export class StudentInfo {
    public name: string;
    public chinese: string;
    public maths: string;
    public english: string;
    constructor() {
        this.name = '';
        this.chinese = '';
        this.maths = '';
        this.english = '';
    }
}

3.服务

services/student-info.service.ts

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { StudentInfo } from '../models/student-info.model';

@Injectable({
  providedIn: 'root'
})
export class StudentInfoService {
  studentInfo_$: Subject<StudentInfo> = new Subject<StudentInfo>();

  constructor() { }
  setStudentInfo(newStudentInfo: StudentInfo) {
    console.log(newStudentInfo);
    this.studentInfo_$.next(newStudentInfo);
  }
  getStudentInfo(newStudentInfo: StudentInfo) {
    this.studentInfo_$.next(newStudentInfo);
  }

}

4.组件获取值

app.component.html

<app-brother-one></app-brother-one>
<app-brother-two></app-brother-two>

brother-one.component.html

<h1>brother-one works!</h1>
<p>显示功能</p>
<p>姓名name:{{studentInfos.name}}</p>
<p>语文chinese:{{studentInfos.chinese}}</p>
<p>数学maths:{{studentInfos.maths}}</p>
<p>英语english:{{studentInfos.english}}</p>

brother-one.component.ts

import { Component, OnInit } from '@angular/core';
import { StudentInfo } from 'src/app/models/student-info.model';
import { StudentInfoService } from 'src/app/services/student-info.service';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-brother-one',
  templateUrl: './brother-one.component.html',
  styleUrls: ['./brother-one.component.css']
})
export class BrotherOneComponent implements OnInit {
  public studentInfos = new StudentInfo();
  public _destroyed$ = new Subject();
  constructor(private studentInfoService:StudentInfoService,) { }

  ngOnInit(): void {
    // 初始化就要广播
    this.getLastedValue();
  }
  //获取最新的值
  getLastedValue() {
    this.studentInfoService.studentInfo_$.pipe(takeUntil(this._destroyed$)).subscribe(data => {
      this.studentInfos = data;
      console.log(this.studentInfos);
    });
  }
  ngOnDestory() {
    this._destroyed$.next();
    this._destroyed$.complete();
  }

}

brother-two.component.html

<h1>brother-two works!</h1>
<p>修改功能</p>
<form [formGroup]="studentForm">
    姓名name:<input type="text" formControlName="name"/><br>
    语文chinese:<input type="text" formControlName="chinese"/><br>
    数学maths:<input type="text" formControlName="maths"/><br>
    英语english:<input type="text" formControlName="english"/><br>
    <button (click)="updateValue()">应用修改</button>
</form>

brother-two.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { StudentInfo } from 'src/app/models/student-info.model';
import { StudentInfoService } from 'src/app/services/student-info.service';

@Component({
  selector: 'app-brother-two',
  templateUrl: './brother-two.component.html',
  styleUrls: ['./brother-two.component.css']
})
export class BrotherTwoComponent implements OnInit {
  public studentForm:FormGroup;
  public studentInfo = new StudentInfo();
  constructor(private fb: FormBuilder,
    private studentInfoService: StudentInfoService) { }

  ngOnInit(): void {
    this.studentForm = this.createFrom();
    console.log(this.studentForm);
  }
  createFrom() {
    return this.fb.group({
      name:[''],
      chinese: [''],
      maths:[''],
      english:['']
    });
  }
  updateValue() {
    console.log(this.studentForm);
    this.studentInfo = this.studentForm.value;
    this.studentInfoService.setStudentInfo(this.studentInfo);
    console.log(this.studentInfo);
  }
 
}

5.效果

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值