我正在构建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>
strategy.service.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:无法在ParamSetComponent.ngOnInit读取未定义的属性“名称”。
答案 0 :(得分:0)
您需要将组件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>