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

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

相关内容

热门资讯

科技新动态“逗娱碰胡开挂”起初... 科技新动态“逗娱碰胡开挂”起初有辅助下载(有挂透视);无需打开直接搜索薇:136704302 咨询了...
热门推荐“中至赣牌圈五十k黑科... 您好:中至赣牌圈五十k黑科技这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
揭秘攻略“宝宝浙江游戏脚本”往... 宝宝浙江游戏脚本开挂教程视频分享装挂详细步骤在当今的网络游戏中,宝宝浙江游戏脚本作为一种经典的娱乐方...
我来教大家“玉海楼茶苑脚本”原... 我来教大家“玉海楼茶苑脚本”原生有辅助开挂下载(有挂分享);无需打开直接搜索加薇136704302(...
总算了解“约局吧辅助”从来有辅... 您好:约局吧辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好...
透视神器“同乡游辅助软件下载”... 同乡游辅助软件下载开挂教程视频分享装挂详细步骤在当今的网络游戏中,同乡游辅助软件下载作为一种经典的娱...
玩家必看科普“兴动互娱工具名称... 【亲,兴动互娱工具名称 这款游戏可以开挂的,确实是有挂的,很多玩家在这款兴动互娱工具名称中打牌都会发...
最新技巧“天天贵州万能辅助器”... 您好:天天贵州万能辅助器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
今日公布“广西老友辅助app”... >>您好:广西老友辅助app确实是有挂的,很多玩家在这款广西老友辅助app游戏中打牌都会发现很多用户...
重大来袭“wepoker永久免... 重大来袭“wepoker永久免费脚本”本然有辅助工具(有挂方针)您好:wepoker永久免费脚本这款...