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、模拟器是什么优化,俱乐...
科技新动态!hhpoker破解... 科技新动态!hhpoker破解工具(辅助挂)好像是有挂(有挂秘籍辅助攻略);1、每一步都需要思考,不...
实测揭晓!佛手在线大菠萝智能辅... 实测揭晓!佛手在线大菠萝智能辅助器(辅助挂)切实是有挂(的确有挂辅助工具)1、该软件可以轻松地帮助玩...
玩家必看科普!hhpoker德... 玩家必看科普!hhpoker德州透视(辅助挂)其实存在有挂(有挂秘籍辅助技巧)1、让任何用户在无需安...
我来分享!aa poker辅助... 我来分享!aa poker辅助(辅助挂)都是有挂(有挂讲解辅助方法)1、辅助器安装包、辅助器2、分享...
重大通报!wepoker透视方... 重大通报!wepoker透视方法(辅助挂)本来存在有挂(有挂头条辅助器)1、完成辅助器v3.3的残局...
一分钟了解!hhpoker可以... 一分钟了解!hhpoker可以控制牌吗(辅助挂)本来是有挂(有挂助手辅助方法)1、有没有辅助教程、有...
科普攻略!wepoker如何设... 科普攻略!wepoker如何设置透视(辅助挂)其实有挂(有挂方略辅助攻略)1、超多福利:超高返利,海...
揭秘真相!菠萝辅助器免费版的特... 揭秘真相!菠萝辅助器免费版的特点(辅助挂)一贯有挂(有挂猫腻辅助工具)辅助器是一种具有地方特色的麻将...
玩家必看科普!wepoker是... 玩家必看科普!wepoker是不是有人用挂(辅助挂)果然真的有挂(真是有挂辅助技巧)小薇(辅助器软件...