Angular路由器。在子组件之间保持状态,响应用户输入。
创始人
2024-10-28 03:33:32
0次

要在Angular路由器中保持状态并响应用户输入,可以使用路由器的参数传递和订阅机制。下面是一个示例解决方法:

  1. 配置路由器:

    // app-routing.module.ts
    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { HomeComponent } from './home.component';
    import { ChildComponent } from './child.component';
    
    const routes: Routes = [
      { path: '', component: HomeComponent },
      { path: 'child/:id', component: ChildComponent }
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    
  2. 在父组件中定义一个共享状态的服务:

    // state.service.ts
    import { Injectable } from '@angular/core';
    import { Subject } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class StateService {
      private stateSource = new Subject();
      state$ = this.stateSource.asObservable();
    
      setState(state: string) {
        this.stateSource.next(state);
      }
    }
    
  3. 在父组件中订阅状态的变化,并在用户输入时更新状态:

    // home.component.ts
    import { Component } from '@angular/core';
    import { StateService } from './state.service';
    
    @Component({
      selector: 'app-home',
      template: `
        

    Home Component

    ` }) export class HomeComponent { state: string; constructor(private stateService: StateService) {} updateState() { this.stateService.setState(this.state); } }
  4. 在子组件中订阅状态的变化,并在状态变化时更新视图:

    // child.component.ts
    import { Component, OnDestroy } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    import { StateService } from './state.service';
    import { Subscription } from 'rxjs';
    
    @Component({
      selector: 'app-child',
      template: `
        

    Child Component

    Parent state: {{ parentState }}

    Child state: {{ childState }}

    ` }) export class ChildComponent implements OnDestroy { parentState: string; childState: string; private stateSubscription: Subscription; constructor( private route: ActivatedRoute, private stateService: StateService ) { this.parentState = this.route.snapshot.paramMap.get('id'); this.stateSubscription = this.stateService.state$.subscribe((state) => { this.childState = state; }); } ngOnDestroy() { this.stateSubscription.unsubscribe(); } }

这样,当用户在父组件中输入文本时,状态会被更新并通过服务传递给子组件,子组件会相应地更新视图。

相关内容

热门资讯

推荐一款!十八楼有挂么(开挂)... 推荐一款!十八楼有挂么(开挂),十八楼竟然真的有开挂技法1)十八楼免费钻石:进一步探索十八楼免费脚本...
一分钟快速了解!丫丫湖南麻将透... 一分钟快速了解!丫丫湖南麻将透视软件(作弊器),丫丫湖南麻将好像是有作弊器总结1、许多玩家不知道丫丫...
推荐几款新版!赣南麻将透视开挂... 推荐几款新版!赣南麻将透视开挂(透视),赣南麻将其实是真的有透视烘培1、超多福利:超高返利,海量正版...
玩家攻略!聚贤辅助工具(透视)... 玩家攻略!聚贤辅助工具(透视),聚贤竟然是真的有透视方案;1、实时聚贤透视辅助更新:用户可以随时随地...
一分钟了解!打两圈泰州麻将能不... 一分钟了解!打两圈泰州麻将能不能开挂(辅助器),打两圈泰州麻将一贯真的有辅助器要领1、操作简单,无需...
实测发现!海洋麻将有没有透视挂... 实测发现!海洋麻将有没有透视挂(作弊器),海洋麻将切实是有作弊器课程1、首先打开海洋麻将辅助器下载最...
六分钟了解!宝讯棋牌透视方法(... 六分钟了解!宝讯棋牌透视方法(辅助器),宝讯棋牌总是是有辅助器策略1、玩家可以在宝讯棋牌透视最简单三...
必看攻略!微乐福州麻将能不能开... 必看攻略!微乐福州麻将能不能开挂(开挂),微乐福州麻将本来是真的有开挂指南书1、微乐福州麻将模拟器是...
带你了解!大唐麻将全集能不能开... 带你了解!大唐麻将全集能不能开挂(辅助器),大唐麻将全集果然存在有辅助器模板1、超多福利:超高返利,...
实测发现!小巷娱乐开挂破解(辅... 实测发现!小巷娱乐开挂破解(辅助挂),小巷娱乐原来存在有辅助挂法门1、小巷娱乐辅助软件下载优化,小巷...