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、丫丫衡阳字牌辅助器安装包、丫...
一分钟了解!27棋牌开挂功能插... 一分钟了解!27棋牌开挂功能插件,27棋牌一贯存在有开挂插件进入游戏-大厅左侧-新手福利-激活码辅助...
记者揭秘!中至赣牌圈开挂功能神... 记者揭秘!中至赣牌圈开挂功能神器,中至赣牌圈确实真的有开挂神器1、下载好中至赣牌圈透视辅助下载之后点...
玩家必用!皮皮斗地主透视挂功能... 玩家必用!皮皮斗地主透视挂功能插件,皮皮斗地主一直真的有透视挂插件1、操作简单,无需皮皮斗地主手机版...
今日公布!海安麻将开挂功能ap... 今日公布!海安麻将开挂功能app,海安麻将原来真的有开挂app一、海安麻将游戏安装教程牌型概率发牌机...
盘点十款!莆仙吹牛开挂功能软件... 盘点十款!莆仙吹牛开挂功能软件,莆仙吹牛总是是有开挂软件莆仙吹牛能透视中分为三种模型:莆仙吹牛模拟器...