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、任何圣盛晃晃麻将透视...
必备教程开挂!嘻游联盟辅助透视... 必备教程开挂!嘻游联盟辅助透视挂,微乐卡五星确实有外挂,果然发现有挂1、下载好微乐卡五星正确养号方法...
现就发布提示透视挂!中至九江辅... 现就发布提示透视挂!中至九江辅助透视挂,妙趣斗地主真的有外挂,都是发现有挂1、在妙趣斗地主插件功能辅...
截至目前开挂!福建天天开心辅助... 截至目前开挂!福建天天开心辅助透视挂,丹东麻将真的是有外挂,真是有挂教程1、点击下载安装,福建天天开...
今日百科透视挂!蜀州麻将辅助透... 今日百科透视挂!蜀州麻将辅助透视挂,微乐海南麻将确实有外挂,一贯有挂秘籍1、下载好微乐海南麻将透视辅...
重要通知透视挂!新蜜瓜大厅辅助... 重要通知透视挂!新蜜瓜大厅辅助透视挂,衡阳丫丫竞技真的是有外挂,一直有挂秘笈1、很好的工具软件,可以...
玩家爆料开挂!慕星棋牌辅助透视... 玩家爆料开挂!慕星棋牌辅助透视挂,同乡游麻将确实有外挂,好像有挂分享1、进入到慕星棋牌是否有挂之后,...
一分钟了解透视挂!微乐河北麻将... 一分钟了解透视挂!微乐河北麻将辅助透视挂,锦绣乐游确实有外挂,都是有挂秘笈1、在微乐河北麻将插件功能...