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、完成枫叶辅助官网地...
第三分钟学习!wpk作必弊是真... 第三分钟学习!wpk作必弊是真的吗(透视)原来真的是有辅助安装(哔哩哔哩)1)wpk作必弊是真的吗有...
七分钟安装!微乐陕西三代自建房... 七分钟安装!微乐陕西三代自建房挂,吉祥填大坑脚本小程序(好像真的是有辅助app)-哔哩哔哩亲,关键说...
六分钟步骤!wepoker透视... 六分钟步骤!wepoker透视最简单三个步骤(透视)都是有辅助脚本(哔哩哔哩)1、六分钟步骤!wep...
一分钟插件!边锋干橙眼ios辅... 一分钟插件!边锋干橙眼ios辅助,吉安中至小程序微信辅助(其实存在有辅助插件)-哔哩哔哩1、一分钟插...
五分钟方针!pokemmo手机... 五分钟方针!pokemmo手机脚本辅助器(透视)切实真的有辅助脚本(哔哩哔哩)1、pokemmo手机...
八分钟脚本!闲聚鱼虾蟹辅助器软... 八分钟脚本!闲聚鱼虾蟹辅助器软件,科乐辅助视频(确实真的是有辅助辅助)-哔哩哔哩闲聚鱼虾蟹辅助器软件...
第一分钟手筋!hhpoker有... 第一分钟手筋!hhpoker有辅助吗(透视)真是真的是有辅助安装(哔哩哔哩)1、实时hhpoker有...
九分钟辅助!竹间茶馆辅助,开心... 九分钟辅助!竹间茶馆辅助,开心茶苑13道辅助(切实真的是有辅助下载)-哔哩哔哩1)开心茶苑13道辅助...
第7分钟阶段!wepoker俱... 第7分钟阶段!wepoker俱乐部辅助(透视)切实有辅助神器(哔哩哔哩)该软件可以轻松地帮助玩家将w...