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

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

相关内容

热门资讯

透视系统"wepok... 透视系统"wepoker好友局透视"详细辅助透明挂教程(私人局辅助教程)是一款可以让一直输的玩家,快...
透视游戏"大菠萝免费... 透视游戏"大菠萝免费辅助器"详细辅助安装教程(透视脚本安卓神器);最新版2024是一款经典耐玩的益智...
透视ai"wepok... 透视ai"wepoker国外版透视"详细辅助2025新版总结(透视方法攻略);建议优先通过wepok...
透视游戏"wepok... 1、透视游戏"wepoker透视脚本是什么"详细辅助分享教程(私人局外卦软件);详细教程。2、wep...
透视教程"wepok... 透视教程"wepoker看底牌"详细辅助必赢教程(有挂神器);小薇(透视辅助)致您一封信;亲爱wep...
透视脚本"wepok... 透视脚本"wepoker破解工具"详细辅助揭秘攻略(俱乐部辅助器神器);致您一封信;亲爱wepoke...
透视中牌率"wepo... 透视中牌率"wepoker数据分析"详细辅助可靠教程(有透视方法神器)1、很好的工具软件,可以解锁游...
透视私人局"德普之星... 透视私人局"德普之星app安卓版破解版"详细辅助揭秘攻略(高级辅助技巧);科技安装教程;136704...
透视好友房"wepo... 1、透视好友房"wepoker辅助器"详细辅助介绍教程(透视脚本免费透视)(UU poker、wep...
辅助透视"wepok... 辅助透视"wepoker分析"详细辅助新版2025教程(辅助机器人透明);1分钟了解详细教程(微信 ...