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免费辅助器(透视)原来是真的辅助插件(有挂实锤)-哔哩哔哩1.hhpoker...
透视教学!we poker游戏... 透视教学!we poker游戏下(透视)切实有辅助修改器(有挂教程)-哔哩哔哩1、让任何用户在无需w...
透视好牌!拱趴游戏破解器(透视... 透视好牌!拱趴游戏破解器(透视)切实是真的辅助插件(竟然有挂)-哔哩哔哩1)拱趴游戏破解器辅助插件:...
近年来!wepoker正确养号... 近年来!wepoker正确养号方法(透视)好像是真的辅助app(有挂详细)-哔哩哔哩1、每一步都需要...
透视了解!哈糖大菠萝有没有挂(... 您好,哈糖大菠萝有没有挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
经核实!hhpoker有后台操... 经核实!hhpoker有后台操作吗(透视)竟然真的是有辅助下载(有挂详情)-哔哩哔哩1、下载好hhp...
无独有偶!wepoker免费透... 无独有偶!wepoker免费透视(透视)真是存在有辅助软件(证实有挂)-哔哩哔哩小薇(辅助器软件下载...
据通报!pokerrrr2辅助... 据通报!pokerrrr2辅助(透视)一直是真的辅助插件(有挂教学)-哔哩哔哩1、很好的工具软件,可...
长期以来!hhpoker是真的... 长期以来!hhpoker是真的吗(透视)其实是真的辅助安装(讲解有挂)-哔哩哔哩1、每一步都需要思考...
透视辅助!wpk辅助器是真的吗... 透视辅助!wpk辅助器是真的吗(透视)其实存在有辅助app(有挂细节)-哔哩哔哩1)wpk辅助器是真...