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、实时来趣广西友玩脚本辅助透...
普及知识!桂林字牌辅助,多乐跑... 普及知识!桂林字牌辅助,多乐跑辅助,详细软件(有挂分享)桂林字牌辅助脚本下载中分为三种模型:桂林字牌...
三分钟了解!微信老友广东辅助器... 三分钟了解!微信老友广东辅助器,朋朋政和麻将为什么一直输,详细器(有挂教程)一、朋朋政和麻将为什么一...
推荐一款!菠萝神辅助器app,... 推荐一款!菠萝神辅助器app,雀友会广东潮汕辅助透视,详细器(新版有挂)1、完成雀友会广东潮汕辅助透...
必备科技!微信小程序微乐辅助器... 必备科技!微信小程序微乐辅助器,老友麻将开挂辅助器,详细神器(有挂总结)1、让任何用户在无需微信小程...
玩家必看教程!jj斗地主麻将外... 玩家必看教程!jj斗地主麻将外挂,欢乐达人正版脚本,详细软件(有挂详细)1、完成欢乐达人正版脚本辅助...
详细说明!微乐河南小程序微乐辅... 详细说明!微乐河南小程序微乐辅助脚本,蛮王大厅辅助插件,详细工具(今日头条)该软件可以轻松地帮助玩家...
专业讨论!传送屋软件辅助,皮皮... 专业讨论!传送屋软件辅助,皮皮透视辅助软件工具,详细软件(有挂秘诀)1、超多福利:超高返利,海量正版...