gpt4 book ai didi

javascript - 从 Angular 中的孙组件访问祖组件属性

转载 作者:行者123 更新时间:2023-11-30 19:24:48 25 4
gpt4 key购买 nike

我正在构建一个 Angular 应用程序,需要访问组件 3 中组件 1 的属性。组件 1 和组件 3 之间的关系是祖孙关系。

我已经成功地直接实现了父/子组件之间的通信(即从组件 1 到组件 2 以及从组件 2 到组件 3(注意组件 3 是组件 2 的子组件,组件 2 是组件 1 的子组件)。我只需要单向通信(即从子组件中的(大)父组件访问属性)。

您可以在下面看到我的应用程序的结构。我也使用共享服务。

组件 1.ts

import { Component, OnInit } from '@angular/core'
import { StrategyService } from './shared/strategy.service'

@Component({
selector: 'strategies-list',
templateUrl: './strategies-list.component.html'
})

export class StrategiesListComponent implements OnInit {
strategies:any[]
constructor(private strategyService: StrategyService) {

}

ngOnInit() {
this.strategies = this.strategyService.getStrategies()
}

}

组件 1.html

<div>
<h1>Strategies</h1>
<hr/>
<strategy-thumbnail *ngFor = "let strategy of strategies" [strategy] = "strategy"> </strategy-thumbnail>
</div>

组件 2.ts

import { StrategyService } from './shared/strategy.service'


@Component ({
selector:'strategy-thumbnail',
templateUrl:'./strategy-thumbnail.component.html',
styles: [`
.pad-left { margin-left: 10px; }
.well div { color: #bbb; }
`]
})

export class StrategyThumbnailComponent implements OnInit {
@Input() strategy:any
psets:any

constructor(private strategyService: StrategyService) {

}

ngOnInit() {
this.psets =this.strategyService.getParameterSets(this.strategy.Name)
}



}

组件 2.html

<div class="well">
{{strategy?.Name}}
<param-set *ngFor = "let pset of psets" [pset] = "pset"> </param-set>

</div>

组件 3.ts

import { Component, Input, OnInit } from '@angular/core'
import { StrategyService } from '../strategies/shared/strategy.service'




@Component ({
selector:'param-set',
templateUrl:'./param-set.component.html'
})

export class ParamSetComponent {
@Input() pset: any
@Input() strategy: any
returns: any




constructor(private strategyService: StrategyService) {

}

ngOnInit() {

this.returns = this.strategyService.getReturns(***SomeStrategyName***,this.pset.Name)
}


}

组件 3.html

<div> {{pset?.Name}} </div>


<return-vector *ngFor = "let return of returns" [return] = "return"> </return-vector>

组件 4.ts

import { Component, Input } from '@angular/core'



@Component ({
selector:'return-vector',
templateUrl:'./return-vector.component.html'
})

export class ReturnVectorComponent {
@Input() strategy:any
@Input() pset: any
@Input() return: any
}

组件 4.html

<div>Period: {{return?.period}}, Return: {{return?.return}}</div>

策略.服务.ts

import { Injectable } from '@angular/core'


@Injectable()

export class StrategyService {
getStrategies() {
return STRATEGIES
}

getStrategy(Name:string) {
return this.getStrategies().find(strat => strat.Name === Name)
}

getParameterSets (Name: string) {
return this.getStrategy(Name).PSETS
}

getParameterSet (StrategyName, PSetName) {
return this.getParameterSets(StrategyName).find(pset => pset.Name === PSetName)
}

getReturns (StrategyName, PSetName) {
return this.getParameterSet(StrategyName, PSetName).Returns
}

getReturn(StrategyName, PSetName, Period) {
return this.getReturns(StrategyName, PSetName).find(returnperiod => returnperiod.period === Period)
}

}

const STRATEGIES = [
{ "Name": "SomeStrategyName1", "PSETS: [{"Name":"SomePSetName1", "Returns": [{ "period": "someperiod1", "return" : somenumber1}, {"period": "someperiod2", "return" : somenumber2}]}, {"Name":"SomePSetName2", "Returns": [{ "period": "someperiod3", "return" : somenumber3}, {"period": "someperiod4", "return" : somenumber4}]}]},

{ "Name": "SomeStrategyName2", "PSETS: [{"Name":"SomePSetName3", "Returns": [{ "period": "someperiod5", "return" : somenumber5}, {"period": "someperiod6", "return" : somenumber6}]}, {"Name":"SomePSetName4", "Returns": [{ "period": "someperiod3", "return" : somenumber3}, {"period": "someperiod4", "return" : somenumber4}]}]},

...

{ "Name": "SomeStrategyNameK", "PSETS: [{"Name":"SomePSetName3", "Returns": [{ "period": "someperiod5", "return" : somenumber5}, {"period": "someperiod6", "return" : somenumber6}]}, {"Name":"SomePSetName4", "Returns": [{ "period": "someperiod3", "return" : somenumber3}, {"period": "someperiod4", "return" : somenumber4}]}]}]

在上面的代码中,除一件事外,一切都按预期工作:在组件 3.ts 中,我想访问一些特定的返回集。如果我输入一些特定的策略名称(例如“SomeStrategyName1”),我的代码就可以工作。但我希望这个策略名称特定于我正在循环的策略。

我尝试用 this.strategy.Name 替换“SomeStrategyName1”,因为我已经两次使用输入参数(一次在组件 3 中,一次在组件 2 中)。在组件 2 中,这有效:在 ts 文件中调用 getParameterSets 函数时,我可以成功访问 this.strategy 的 Name 属性。

然而,在组件 3 中,这不起作用。我收到 TypeError: Cannot read property 'Name' of undefined at ParamSetComponent.ngOnInit。

最佳答案

您需要将组件 2 模板中的 strategy 传递给 param-set 组件,因为它需要一个:

<div class="well">
{{strategy?.Name}}
<param-set
*ngFor="let pset of psets"
[pset]="pset"
[strategy]="strategy">
</param-set>
</div>

您还必须对组件 3 等执行相同的操作...

<div> {{pset?.Name}} </div>
<return-vector
*ngFor="let return of returns"
[pset]="pset"
[strategy]="strategy"
[return]="return">
</return-vector>

关于javascript - 从 Angular 中的孙组件访问祖组件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57028508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com