Я работаю над проектом Angular с маршрутизацией. Я уже связываюсь из маршрута с компонентом приложения и обновляю его значение там. Однако, когда я пытаюсь отправить эту информацию на другой маршрут (который может не отображаться в браузере), я не могу получить к нему доступ. Мне просто нужна эта информация в этом конкретном маршруте, и я хотел бы go с помощью любого метода, который может это сделать.
В app.component.ts у меня есть
import { Component, OnInit, OnDestroy } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Subscription } from 'rxjs';
import { MessageService } from './message.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy{
public message = "Discover Workouts";
public state:boolean = false;
subscription: Subscription;
//Inject MessageService
constructor(private messageService: MessageService) {
this.subscription = this.messageService.getMessage()
.subscribe(mymessage => this.message = mymessage)
}
ngOnInit(){
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
In workout- options.ts У меня это
import { Component, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { MessageService } from '../message.service';
import { Sio2service } from '../sio2.service';
@Component({
selector: 'app-workout-options',
templateUrl: 'workout-options.component.html',
styleUrls: ['../app.component.css']
})
export class WorkoutOptionsComponent implements OnDestroy{
mymessage: string;
subscription: Subscription;
@Output() public childEvent = new EventEmitter();
constructor(private messageService: MessageService) {
this.subscription = this.messageService.getMessage()
.subscribe(mymessage => { this.mymessage = mymessage; });
}
OnInit(){
}
FupdateParent(exerName:string) {
this.messageService.updateMessage(exerName);
// this.sio2.sendMsg(exerName);
//console.log(exerName);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
, а в message.service -
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class MessageService {
private myMessage = new Subject<string>();
updateMessage(message: string) {
this.myMessage.next(message);
}
clearMessage() {
this.myMessage.next();
}
getMessage(): Observable<string> {
return this.myMessage.asObservable();
}
}
Пока все работает нормально. Но когда я пытаюсь получить в start-workout.ts (который реализован как маршрут) значение, отправленное последним компонентом (который также реализован как маршрут), я не могу.
Вот что я пробовали.
В start-workout.component.ts у меня есть это (часть кода)
export class StartWorkoutComponent implements OnInit{
public message = "";
subscription: Subscription;
constructor(private messageService: MessageService, private httpClient: HttpClient){
this.subscription = this.messageService.getMessage()
.subscribe(mymessage => { this.message = mymessage; });
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
Я должен делать что-то не так. Может ли кто-нибудь помочь мне