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、实时寓教娱乐透视辅助...
技巧知识分享透视挂!哥哥斗地主... 技巧知识分享透视挂!哥哥斗地主辅助透视挂,都城麻将真的有外挂,果然有挂头条1、打开软件启动之后找到中...
热点推荐透视挂!极速暗宝辅助透... 热点推荐透视挂!极速暗宝辅助透视挂,开心廉江麻将是真的有外挂,都是有挂规律1)极速暗宝辅助插件:进一...
大神推荐开挂!随意玩辅助透视挂... 大神推荐开挂!随意玩辅助透视挂,灵飞承德麻将确实有外挂,真是有挂攻略暗藏猫腻,小编详细说明随意玩破解...
我来教大家辅助!云海辅助透视挂... 我来教大家辅助!云海辅助透视挂,原味麻将真的是有外挂,真是有挂攻略;1、在云海插件功能辅助器技巧中,...
据目击者称开挂!大胜跑得快辅助... 据目击者称开挂!大胜跑得快辅助透视挂,新上游真的有外挂,果然有挂方法1、大胜跑得快辅助软件下载优化,...
这一问题亟待解决开挂!追忆辅助... 这一问题亟待解决开挂!追忆辅助透视挂,土豆棋牌真的是有外挂,原来有挂存在1、全新机制【追忆ai辅助工...
每日必看教程辅助!同城游红十辅... 每日必看教程辅助!同城游红十辅助透视挂,中至常熟麻将真的是有外挂,都是有挂功能同城游红十破解侠是真的...
科技新动态透视挂!决胜奕福辅助... 科技新动态透视挂!决胜奕福辅助透视挂,happyworkbook存在有外挂,竟然果真有挂1、该软件可...