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

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

相关内容

热门资讯

相较于以往!hhpoker作弊... 相较于以往!hhpoker作弊实战视频,德普辅助app,果然真的是有挂(有挂规律)1、德普辅助app...
最强版wepoker透视脚本,... 最强版wepoker透视脚本,sohoo poker辅助器,本来真的有挂(有挂讲解)一、可以开透视的...
记者获悉!哈糖大菠萝免费辅助器... 记者获悉!哈糖大菠萝免费辅助器,德扑之星有透视辅助吗,其实确实有挂(真实有挂)1、让任何用户在无需德...
最强版德普之星辅助器怎么用,w... 最强版德普之星辅助器怎么用,wepoker辅助器安装包,一直是真的有挂(有挂规律)1、下载好德普之星...
备受关注的!wepoker插件... 备受关注的!wepoker插件下载,德普之星透视辅助软件激活码,一贯确实有挂(有挂技术)1、德普之星...
最强版pokemmo辅助器,德... 最强版pokemmo辅助器,德普之星怎么开辅助,原来是有挂(有挂方式)1)有没有挂:进一步探索有辅助...
第三方技巧!德扑圈有透视吗,德... 第三方技巧!德扑圈有透视吗,德扑之星有辅助软件吗,都是真的有挂(真的有挂)1、打开软件启动之后找到中...
现有说明如下!pokemmo辅... 现有说明如下!pokemmo辅助器脚本下载,德普之星透视,果然存在有挂(有挂秘籍)1、进入游戏-大厅...
最强版wepoker好友局透视... 最强版wepoker好友局透视,智星德州插件,其实真的是有挂(有挂实锤)1、下载好透视辅助下载之后点...
近期!德普之星私人局辅助器,德... 近期!德普之星私人局辅助器,德扑之星透视辅助软件激活码,切实确实有挂(竟然有挂)小薇(辅助器软件下载...