Как обновить приложение Angular 7 удаленно во второй вкладке браузера - PullRequest
0 голосов
/ 15 июня 2019

Мое приложение показывает, что некоторые новости открыты, одно и то же приложение на двух ПК. Мне нужно иметь возможность обновлять все экраны удаленно. Я использую тему в качестве наблюдаемой при работе с Angular 7.

Я попытался обновить второй экран с помощью make .next () на первом экране

app.component.html

<button (click)="onClickRefresh()" type="button">refrech</button>
<p>{{teststr}}</p>
<router-outlet></router-outlet>

app.component.ts

import {Component, OnDestroy, OnInit} from '@angular/core';
import { SharedService } from './shared.service';
import {Subscription} from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit, OnDestroy{
  teststr: string;
  private refreshNeeded: Subscription;

  constructor( private sharedService: SharedService) {
    this.teststr = 'emptyyyyy';
  }

  ngOnInit() {
    this.refreshNeeded = this.sharedService
      .getRefreshNeeded()
      .subscribe( () => {
        // location.reload();
        this.teststr = 'loaded';
    });
  }

  onClickRefresh() {
    this.sharedService.letRefreshNow();
  }

  ngOnDestroy() {
    if (this.refreshNeeded) {
    this.refreshNeeded.unsubscribe();
    }
  }

}

shared.service.ts

import { Injectable } from '@angular/core';
import {Subject} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SharedService {
  private refreshNeeded = new Subject<void>();

  constructor() { }

  getRefreshNeeded() {
    return this.refreshNeeded.asObservable();
  }

  letRefreshNow() {
    this.refreshNeeded.next();
  }


}

...