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

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

相关内容

热门资讯

外挂绝活儿!德扑圈透视,pok... 外挂绝活儿!德扑圈透视,pokernow辅助控制-好像是有辅助神器(哔哩哔哩)1、pokernow辅...
外挂机巧!哈糖大菠萝有挂吗,p... 外挂机巧!哈糖大菠萝有挂吗,pokeplus脚本-切实有辅助软件(哔哩哔哩)1、打开软件启动之后找到...
外挂秘籍!如何下载德普之星辅助... 外挂秘籍!如何下载德普之星辅助软件,大菠萝免费辅助-真是存在有辅助工具(哔哩哔哩)1、进入到大菠萝免...
外挂法子!pokerworld... 外挂法子!pokerworld辅助器,德普之星透视免费-真是是有辅助工具(哔哩哔哩)1、pokerw...
外挂讲义!德州透视竞技联盟,佛... 外挂讲义!德州透视竞技联盟,佛手大菠萝辅助-一贯是真的有辅助app(哔哩哔哩)1、该软件可以轻松地帮...
外挂妙招!菠萝德州透视脚本,哈... 外挂妙招!菠萝德州透视脚本,哈糖大菠萝有挂吗-好像一直总是有辅助软件(哔哩哔哩)1、该软件可以轻松地...
外挂练习!线上德州的辅助器是什... 外挂练习!线上德州的辅助器是什么,拱趴大菠萝辅助神器-一直一直都是有辅助软件(哔哩哔哩)1、起透看视...
外挂办法!大菠萝免费辅助器,p... 外挂办法!大菠萝免费辅助器,pokerrrr2辅助-切实是有辅助插件(哔哩哔哩)1、进入到大菠萝免费...
外挂讲义!拱趴游戏破解器,we... 外挂讲义!拱趴游戏破解器,werplan免费挂下载-总是是真的有辅助工具(哔哩哔哩)小薇(辅助器软件...
外挂妙招!线上德州的辅助器是什... 外挂妙招!线上德州的辅助器是什么,德州透视插件-都是有辅助插件(哔哩哔哩)1)线上德州的辅助器是什么...