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)微信小程序里的...
反观!潮友会app下载安卓辅助... 反观!潮友会app下载安卓辅助软件,超圣大厅可控是什么意思-果然存在有辅助攻略(哔哩哔哩)1、潮友会...
一直以来!大菠萝手游辅助,心悦... 一直以来!大菠萝手游辅助,心悦提坑神器软件下载-本来确实有辅助技巧(哔哩哔哩)1.心悦提坑神器软件下...
备受关注的!福建13水插件,广... 备受关注的!福建13水插件,广西友乐软件辅助-竟然存在有辅助脚本(哔哩哔哩)1、很好的工具软件,可以...
透视工具!闲逸碰胡辅助软件下载... 透视工具!闲逸碰胡辅助软件下载,欢乐达人正版脚本-切实真的是有辅助神器(哔哩哔哩)1、点击下载安装,...
2026版复盘!极速摇一摇鱼虾... 2026版复盘!极速摇一摇鱼虾蟹辅助,情怀七喜游戏辅助-一直真的是有辅助攻略(哔哩哔哩)1、极速摇一...
透视肯定!开心泉州免费辅助,衢... 透视肯定!开心泉州免费辅助,衢州都莱罗松辅助器-一贯是有辅助教程(哔哩哔哩);1、透视肯定!开心泉州...
辅助透视!哈灵脚本辅助最简单三... 辅助透视!哈灵脚本辅助最简单三个步骤,樱花之盛挂-一贯是有辅助软件(哔哩哔哩)1、哈灵脚本辅助最简单...
透视好友房!多乐手游辅助,红茶... 透视好友房!多乐手游辅助,红茶楼互娱辅助-好像存在有辅助神器(哔哩哔哩)1、下载好红茶楼互娱辅助脚本...