[英]Get value from one component and use it in another.
I have two component files in my Angular 2 app - home.ts
and folder-selector.ts
. 我的Angular 2应用程序中有两个组件文件
home.ts
和folder-selector.ts
。 I have a variable inside folder-selector.ts called pathToNode
. 我在folder-selector.ts内部有一个名为
pathToNode
的变量。 How do I access this value from home.ts
? 如何从
home.ts
访问此值? My home component has an import for FolderSelectorService - I originally thought that maybe I would need to create a service call to reference pathToNode but that doesn't seem right. 我的home组件具有FolderSelectorService的导入-我本来以为我可能需要创建一个服务调用来引用pathToNode,但这似乎不正确。 Basically how do I get component values between components?
基本上,我如何获取组件之间的组件值? Am I supposed to have an import statement for the folder-selector component?
我应该为文件夹选择器组件添加导入语句吗?
Passing data to (child) components should be done through @Input
, read https://toddmotto.com/passing-data-angular-2-components-input and https://angular.io/docs/ts/latest/cookbook/component-communication.html 将数据传递到(子)组件应该通过
@Input
完成,请阅读https://toddmotto.com/passing-data-angular-2-components-input和https://angular.io/docs/ts/latest/cookbook /component-communication.html
It's important to get familiar with data flows in Angular 2 to create apps, data flows down through @Input
, events flow up through @Output
熟悉Angular 2中的数据流以创建应用程序很重要,数据通过
@Input
向@Input
,事件通过@Output
向上流
You can only pass data down in your component tree, so this requires for your home
component to be nested inside folder-selector
. 您只能将数据向下传递到组件树中,因此这需要将
home
组件嵌套在folder-selector
。 (If that is not the case, you'd have to create a parent component which holds the data) (如果不是这种情况,则必须创建一个保存数据的父组件)
<folder-selector>
<home [pathToNode]="pathToNode"> ...</home>
</folder-selector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.