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

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

相关内容

热门资讯

科技揭秘!四川途游小程序辅助,... 科技揭秘!四川途游小程序辅助,hh poker插件下载,透牌教程(有挂教学);详细四川途游小程序辅助...
来一盘!微信小程序透视挂,we... 来一盘!微信小程序透视挂,wepoker黑侠辅助器,德州教程(确实有挂);致您一封信;亲爱微信小程序...
总算了解!爱玩联盟辅助软件视频... 总算了解!爱玩联盟辅助软件视频讲解,破解辅助插件wepoker,揭秘攻略(真的有挂);大家肯定在之前...
每日必看教程!途游手游辅助,p... 每日必看教程!途游手游辅助,pokemmo脚本辅助器,透明教程(有挂教程);人气非常高,ai更新快且...
分享一款!蜀山四川麻亲友房祈福... 分享一款!蜀山四川麻亲友房祈福,竞技联盟辅助插件,辅助教程(存在有挂)是一款可以让一直输的玩家,快速...
分享开挂内幕!胡乐辅助脚本,w... 您好,胡乐辅助脚本这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款...
总算清楚!广西老友玩有破解视频... 总算清楚!广西老友玩有破解视频,we-poker辅助软件教程,wepoke教程(有挂存在);1、这是...
今日重大通报!南宁友乐辅助,w... 今日重大通报!南宁友乐辅助,wepoker作弊辅助,插件教程(有挂辅助);南宁友乐辅助免费下载原版,...
实测分享!皮皮跑胡子系统有鬼,... 实测分享!皮皮跑胡子系统有鬼,epoker透视底牌,wpk教程(有挂技巧);1、选手技艺的积累:德州...
推荐十款!微乐小程序游戏破解器... 推荐十款!微乐小程序游戏破解器下载,德普之星透视,科技教程(存在有挂);人气非常高,ai更新快且高清...