简体   繁体   English

从一个组件中获取价值,并在另一个组件中使用它。

[英]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.tsfolder-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-inputhttps://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.

相关问题 如何从一个表中获取 id 并将其插入到另一个表中。 postgresql - how to get id from one table and insert it into another. postgresql 如何从一个组件到另一个组件获取价值? - How to get value from one component to another? 寻找一种简单的方法来从一个 HTML 页面中获取值并将其显示在另一个页面中。 下面的例子: - Looking for a simple way to grab a value from one HTML page and have it appear in another. example below: JWT 来自一个网站的无效签名,但另一个网站没有错误。 用户对象的额外属性被添加到令牌中 - JWT Invalid signature from one website, but no errors with another. Extra properties of user object get added to token 将一个图像淡入另一个图像。 - Fade one image into another. 从一个函数发送一个随机数,以供另一个函数使用。 - Send a random number from one function to be used by another. 将元素从一个数组移动到另一个数组。 (JavaScript) - Moving elements from one array to another. (JavaScript) 在一个组件中使用值并在另一个组件中重置 - Use value in one component and reset in another 角度2。无法将一个组件插入另一个。 模板解析错误:...不是已知元素 - angular 2. Cannot insert one component to another. Template parse errors:…is not a known element React:如何从一个组件获取输入值以在另一个组件中进行Ajax调用? - React : how to get input value from one component to make the ajax call in another component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM