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(); } }

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

相关内容

热门资讯

玩家必看分享!开心十三张有没有... 玩家必看分享!开心十三张有没有挂,wepoker开脚本视频,AA德州教程(有挂方法);开心十三张有没...
9分钟了解!小程序游戏修改器辅... 【福星临门,好运相随】;9分钟了解!小程序游戏修改器辅助,哈糖大菠萝有挂吗,安装教程(真的有挂);暗...
专业讨论!九九联盟辅助,德州局... 专业讨论!九九联盟辅助,德州局HHpoker透视脚本,技巧教程(有挂技巧);专业讨论!九九联盟辅助,...
揭秘真相!荆州晃晃麻将圣盛辅助... 揭秘真相!荆州晃晃麻将圣盛辅助,哈糖大菠萝能开挂吗,软件教程(讲解有挂);揭秘真相!荆州晃晃麻将圣盛...
我来教大家!牵手辅助神器下载,... 我来教大家!牵手辅助神器下载,竞技联盟透视插件,科技教程(的确有挂);大家肯定在之前牵手辅助神器下载...
技术分享!威信茶馆透视挂,云扑... 技术分享!威信茶馆透视挂,云扑克有透视吗,辅助教程(有挂方略)是一款可以让一直输的玩家,快速成为一个...
一分钟教你!潮友辅助开挂,wp... 一分钟教你!潮友辅助开挂,wpk系统是否存在作弊行为,攻略教程(有挂技巧);潮友辅助开挂软件透明挂是...
玩家必看秘籍!奇迹陕西靖边打锅... 玩家必看秘籍!奇迹陕西靖边打锅子辅助,wpk透视辅助靠谱吗,教你攻略(有挂详细)是一款可以让一直输的...
科技通报!七千在线辅助,德州之... 科技通报!七千在线辅助,德州之星扫描器,教你攻略(有挂解密);1、点击下载安装,七千在线辅助插件透视...
新手必备!欢乐情怀辅助器,红龙... 新手必备!欢乐情怀辅助器,红龙poker作弊指令,微扑克教程(有挂解惑);欢乐情怀辅助器软件透明挂是...