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、每一步都需要思考,不同水平...
事发当天!wepoker代打辅... 事发当天!wepoker代打辅助,德扑之星透视软件免费入口官网,本来真的有挂(真的有挂);1、首先打...
实操分享!微信小程序游戏辅助,... 实操分享!微信小程序游戏辅助,都莱软件安装辅助,详细技巧(有挂分享)1、每一步都需要思考,不同水平的...
刚刚!wepoker插件下载,... 刚刚!wepoker插件下载,德扑之星辅助器怎么用,好像是有挂(证实有挂)德扑之星辅助器怎么用透视方...
最新消息!wpk透视插件,德普... 最新消息!wpk透视插件,德普之星的辅助工具介绍,都是真的有挂(确实有挂)1、让任何用户在无需德普之...
今日公布!竞技联盟辅助,丽水欢... 今日公布!竞技联盟辅助,丽水欢乐堂辅助插件,详细脚本(有挂方针)1、首先打开丽水欢乐堂辅助插件辅助器...
科普分享!宁夏划水辅助,青鸟辅... 科普分享!宁夏划水辅助,青鸟辅助安卓,详细教程(有挂方法)1、完成青鸟辅助安卓辅助器v3.3的残局,...
反观!uupoker透视,智星... 反观!uupoker透视,智星菠萝有挂吗,好像真的有挂(证实有挂)1、智星菠萝有挂吗脚本辅助下载、智...
一分钟了解!老友广东辅助器,钱... 一分钟了解!老友广东辅助器,钱塘十水三挂件,详细挂(有挂详情)1)钱塘十水三挂件辅助插件:进一步探索...