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

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

相关内容

热门资讯

规律辅助挂!微信牵手跑辅助&q... 规律辅助挂!微信牵手跑辅助"切实存在有辅助技巧"(哔哩哔哩)1、完成微信牵手跑辅助辅助器v3.3的残...
据权威媒体报道!菠萝德州透视脚... 据权威媒体报道!菠萝德州透视脚本"原来确实有辅助挂"(哔哩哔哩)1、菠萝德州透视脚本破解器简单,菠萝...
围绕透视问题!凑一桌开挂游戏辅... 围绕透视问题!凑一桌开挂游戏辅助"果然真的是有辅助挂"(哔哩哔哩)1、下载好凑一桌开挂游戏辅助脚本下...
连日来!逍遥湖北微信辅助&qu... 连日来!逍遥湖北微信辅助"竟然是有辅助挂"(哔哩哔哩)1、逍遥湖北微信辅助免费脚本咨询教程、逍遥湖北...
随着!微信小程序家乡大贰辅助器... 随着!微信小程序家乡大贰辅助器"其实真的是有辅助软件"(哔哩哔哩)1、任何微信小程序家乡大贰辅助器透...
值得注意的是!九九联盟后台可以... 值得注意的是!九九联盟后台可以操作吗"好像真的有辅助插件"(哔哩哔哩)1)九九联盟后台可以操作吗有没...
据悉!衢州都莱罗松怎么老是输&... 据悉!衢州都莱罗松怎么老是输"竟然真的有辅助挂"(哔哩哔哩)所有人都在同一条线上,像星星一样排成一排...
长期以来!白金岛放炮罚开挂&q... 长期以来!白金岛放炮罚开挂"一直存在有辅助神器"(哔哩哔哩)1、超多福利:超高返利,海量正版游戏,白...
复盘辅助挂!正宗牛总管辅助&q... 复盘辅助挂!正宗牛总管辅助"确实确实有辅助神器"(哔哩哔哩)暗藏猫腻,小编详细说明正宗牛总管辅助破解...
不少玩家反映!蜀山四川小程序辅... 不少玩家反映!蜀山四川小程序辅助开挂"原来是真的有辅助教程"(哔哩哔哩)一、蜀山四川小程序辅助开挂可...