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

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

相关内容

热门资讯

玩家必看攻略!奕趣开挂脚本软件... 玩家必看攻略!奕趣开挂脚本软件,奕趣都是存在有开挂揭幕插件奕趣破解侠是真的助透视。每个模型中均包含「...
一分钟揭秘!闲逸麻将透视挂脚本... 一分钟揭秘!闲逸麻将透视挂脚本软件,闲逸麻将真是真的是有透视挂详情插件运闲逸麻将辅助工具,进入游戏界...
玩家必看科普!66徐州麻将透视... 玩家必看科普!66徐州麻将透视脚本软件,66徐州麻将总是真的是有透视科普神器1、完成66徐州麻将有辅...
发现一款!牵手跑胡子透视挂脚本... 发现一款!牵手跑胡子透视挂脚本软件,牵手跑胡子果然真的有透视挂揭幕插件1、上手简单,内置详细流程视频...
技术分享!穿过夏天的风辅助器脚... 技术分享!穿过夏天的风辅助器脚本软件,穿过夏天的风原来是有辅助器专业方法1、该软件可以轻松地帮助玩家...
实测交流!胡乐白银麻将辅助脚本... 实测交流!胡乐白银麻将辅助脚本软件,胡乐白银麻将一贯真的有辅助分享app;1、胡乐白银麻将透视辅助软...
技巧知识分享!鹤岗麻将辅助器脚... 技巧知识分享!鹤岗麻将辅助器脚本软件,鹤岗麻将总是真的是有辅助器揭幕软件1、点击下载安装,鹤岗麻将脚...
分享给玩家!风月透视挂脚本软件... 分享给玩家!风月透视挂脚本软件,风月一直是有透视挂分享教程风月能透视中分为三种模型:风月模拟器、风月...
分享一款!纸蛙盐城麻将辅助脚本... 分享一款!纸蛙盐城麻将辅助脚本软件,纸蛙盐城麻将都是真的有辅助总结攻略1、玩家可以在纸蛙盐城麻将透视...
实测揭晓!畅游互动透视脚本软件... 实测揭晓!畅游互动透视脚本软件,畅游互动原来有透视揭露神器1、点击下载安装,畅游互动脚本分类一目了然...