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

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

相关内容

热门资讯

一分钟教会你辅助!赖子天津麻将... 一分钟教会你辅助!赖子天津麻将ai辅助多功能插件,一直真的有辅助攻略方针是一款可以让一直输的玩家,快...
2027版透视挂!渝都麻将透视... 2027版透视挂!渝都麻将透视挂多功能AI工具,渝都麻将一直真的是有透视挂工具1、在渝都麻将插件功能...
一分钟揭秘!辅助器!广麻ai辅... 一分钟揭秘!辅助器!广麻ai辅助器多功能插件,竟然真的是有辅助器脚本绝活是一款可以让一直输的玩家,快...
2027版作弊器!中至二人麻将... 2027版作弊器!中至二人麻将作弊器多功能AI工具,中至二人麻将好像是有作弊器工具1、中至二人麻将破...
2027版作弊器!边锋绍兴麻将... 2027版作弊器!边锋绍兴麻将作弊器多功能AI脚本,边锋绍兴麻将确实真的是有作弊器脚本1、许多玩家不...
玩家必看分享开挂!粤麻圈ai开... 玩家必看分享开挂!粤麻圈ai开挂多功能插件,竟然存在有开挂助手烘培是一款可以让一直输的玩家,快速成为...
2027版辅助!红心无锡麻将辅... 您好,红心无锡麻将这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
我来向大家传授开挂!潮友潮汕麻... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成潮友潮汕麻将解析、 AI 处理、潮友...
推荐十款透视挂!盘锦麻将ai透... 很多人使用云端 AI 时,都担心盘锦麻将、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技,很...
2027版透视挂!良人麻将透视... 2027版透视挂!良人麻将透视挂多功能AI教程,良人麻将真是是有透视挂教程暗藏猫腻,小编详细说明良人...