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

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

相关内容

热门资讯

热门推荐!微信小程序多功能辅助... 热门推荐!微信小程序多功能辅助,盛世辅助器,详细app(有挂神器)1、实时微信小程序多功能辅助透视辅...
最新技巧!黑科技微乐小程序辅助... 最新技巧!黑科技微乐小程序辅助器免费,随意玩工具箱辅助器,详细教程(真是有挂)1、点击下载安装,黑科...
盘点十款!乐乐围棋入门辅助,小... 盘点十款!乐乐围棋入门辅助,小程序游戏赴沪期,详细app(有挂教学)1、首先打开小程序游戏赴沪期辅助...
实操分享!新超凡手游辅助,雀友... 实操分享!新超凡手游辅助,雀友会广东潮汕麻雀万能辅助器,详细挂(有挂教程)1、全新机制【雀友会广东潮...
一起来探讨!纳祥游戏脚本,微信... 一起来探讨!纳祥游戏脚本,微信小程序辅助工具,详细攻略(有挂解惑)1、这是跨平台的微信小程序辅助工具...
一分钟揭秘!新八戒辅助,挂是真... 一分钟揭秘!新八戒辅助,挂是真的假的,详细器(确实有挂)1、全新机制【挂是真的假的ai辅助工具激活码...
最新通报!微信小程序微乐房间怎... 最新通报!微信小程序微乐房间怎么开挂,打哈儿床将辅助最新,详细软件(有挂技巧)1、全新机制【微信小程...
玩家实测!欢乐达人暗堡破解,赣... 您好,欢乐达人暗堡破解这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
一分钟揭秘!情怀手机麻将辅助器... 一分钟揭秘!情怀手机麻将辅助器,功夫川麻辅助,详细工具(详细教程)1、金币登录送、破产送、升级送、活...
今日科普!微信广东雀神挂件辅助... 今日科普!微信广东雀神挂件辅助,对战互娱辅助系统,详细神器(有挂规律);1、下载好微信广东雀神挂件辅...