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

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

相关内容

热门资讯

6分钟详情!星悦福建麻将到底有... 6分钟详情!星悦福建麻将到底有没有挂,线上德州辅助工具有哪些(详细透视辅助助手教程)1、每一步都需要...
2分钟规律!财神十三张有辅助吗... 2分钟规律!财神十三张有辅助吗,wepoke软件透明挂下载(详细透视辅助脚本教程)1、让任何用户在无...
三分钟辅助挂!嫩江和和麻将真有... 三分钟辅助挂!嫩江和和麻将真有挂吗,wpk长期盈利打法(详细透视辅助神器教程)是一款可以让一直输的玩...
2分钟教程!艾牛娱乐有挂吗,微... 2分钟教程!艾牛娱乐有挂吗,微扑克有辅助(详细透视辅助工具教程)科技教程也叫必备教程,这是一款功能强...
五分钟技巧!福建十三水辅助器下... WePoker透视辅助版本稳定性对比与推荐‌:五分钟技巧!福建十三水辅助器下载,德扑之星专业代打(详...
1分钟详情!中至余干可以装外 ... 《1分钟详情!中至余干可以装外 挂么,德扑牌型胜率(详细透视辅助神器教程)》 中至余干可以装外 挂么...
4分钟介绍!渤海麻将有挂吗,p... WePoker透视辅助工具核心要点解析‌,4分钟介绍!渤海麻将有挂吗,pokermaster有德州(...
4分钟技巧!乐逍遥跑胡子软件,... 4分钟技巧!乐逍遥跑胡子软件,德扑之星能看到底牌(详细透视辅助黑科技教程);亲真的是有正版授权,小编...
一分钟方法!楚天游赤壁打滚辅助... 一分钟方法!楚天游赤壁打滚辅助器,aapoker到底有挂(详细透视辅助助手教程);人气非常高,ai更...
十分钟教程!指尖跑得快小程序辅... 十分钟教程!指尖跑得快小程序辅助,德州wpk德州真的(详细透视辅助神器教程)1、许多玩家不知道指尖跑...