Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:从管线更改父元件的字段_Angular_Typescript_Angular10 - Fatal编程技术网

Angular 角度:从管线更改父元件的字段

Angular 角度:从管线更改父元件的字段,angular,typescript,angular10,Angular,Typescript,Angular10,各位! 我需要你的帮助。我的Angular 10应用程序中有一个组件,我们把它命名为a 在这个组件中,我有,还有routes.ts文件中描述的路由 我需要的是通过单击某个B组件中的按钮来更改组件内的变量(字段),即内的路由。我怎么做 例如,我们可以讨论这种变体 A.component.html <B [changeFieldOfA]="func"></B> <router-outlet></router-outlet> 这里

各位!

我需要你的帮助。我的Angular 10应用程序中有一个组件,我们把它命名为a

在这个组件中,我有
,还有routes.ts文件中描述的路由

我需要的是通过单击某个B组件中的按钮来更改组件内的变量(字段),即
内的路由。我怎么做

例如,我们可以讨论这种变体

A.component.html

<B [changeFieldOfA]="func"></B>
<router-outlet></router-outlet>
这里的一切都很酷,因为我可以传递函数,将我的A组件字段更改为我的组件

但如果我有这个变种呢

A.component.html

<B [changeFieldOfA]="func"></B>
<router-outlet></router-outlet>
我想调用这个func(),它属于B中的A(并更改其字段),但我不能再通过Input()进行调用,因为在路由器中我无法进行调用。

您可以使用服务在组件之间进行通信

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class ShareService  {   
  constructor() { } 
  private paramSource = new BehaviorSubject("");
  sharedData = this.paramSource.asObservable();
  setParam(param:string) { this.paramSource.next(param)}    
}
启用

this.shareService.setParam('Sending param');
从服务中获得

 this.shareService.sharedData.subscribe(data=> { this.your_variable=data })
建设者喜欢

constructor(private shareService: ShareService  ){}

我很难理解。B是a的子组件吗?还是一个新的路由到组件?如果路由到B,则不再查看A,因此无法更改A中的变量。您的问题有代码说明。实际代码要好得多。也许angular还有其他用途,但你不应该在app.component.html文件中只使用路由器插座吗?我不熟悉应用程序有多个路由器插座的想法。看起来很混乱,没有必要