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

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

相关内容

热门资讯

重大科普!wpk俱乐部辅助器(... 重大科普!wpk俱乐部辅助器(辅助挂)竟然有挂(有挂技术辅助插件)小薇(辅助器软件下载)致您一封信;...
如何分辨真伪!如何下载wpk透... 如何分辨真伪!如何下载wpk透视版(辅助挂)原来真的是有挂(有挂攻略辅助插件)1、免费辅助多个强度级...
揭秘几款!wepoker看底牌... 揭秘几款!wepoker看底牌(辅助挂)真是是真的有挂(有挂详情辅助工具)破解侠是真的助透视。每个模...
一分钟揭秘!pokemomo辅... 一分钟揭秘!pokemomo辅助工具(辅助挂)都是是有挂(有挂存在辅助教程)能透视中分为三种模型:模...
最新技巧!wepokerplu... 最新技巧!wepokerplus脚本(辅助挂)都是是真的有挂(果真有挂辅助工具)1、游戏颠覆性的策略...
分享个大家!wepoker高级... 分享个大家!wepoker高级辅助(辅助挂)好像是真的有挂(有挂攻略辅助软件)1、模拟器是什么优化,...
教学盘点!wpk显示有作弊(辅... 教学盘点!wpk显示有作弊(辅助挂)竟然真的是有挂(有挂分享辅助方法)1、玩家可以在透视最简单三个步...
重磅来袭!wejoker内置辅... 重磅来袭!wejoker内置辅助(辅助挂)果然真的有挂(有挂头条辅助攻略)1、该软件可以轻松地帮助玩...
重大通报!wepoker怎么开... 重大通报!wepoker怎么开辅助(辅助挂)一贯真的有挂(有挂攻略辅助器)1、任何透视是真的假的的玩...
推荐十款!poker worl... 推荐十款!poker world辅助器(辅助挂)确实是有挂(真实有挂辅助教程)1)免费钻石:进一步探...