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、皮皮游戏辅助平台公共底牌简单,皮...
我来分享!摸一把麻将辅助,赣南... 我来分享!摸一把麻将辅助,赣南好友辅助,详细挂(有挂功能)我来分享!摸一把麻将辅助,赣南好友辅助,详...
玩家实测!微信小程序蜀山四川辅... 您好,微信小程序蜀山四川辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
一分钟教你!微友助手破解版,广... 一分钟教你!微友助手破解版,广东雀神辅助器透视,详细挂(有挂攻略);1、广东雀神辅助器透视辅助器安装...