简体   繁体   English

将 api 响应从一个组件传递到 Angular 中的另一个组件

[英]Pass api response from one component to another component in Angular

I have a component named server and another component named process.我有一个名为 server 的组件和另一个名为 process 的组件。 I am taking response from server.service.ts to server.component.ts and I want that response in process.component.ts.我正在从 server.service.ts 接收响应到 server.component.ts,我希望在 process.component.ts 中得到响应。 Any ideas on how this can be achieved.关于如何实现这一点的任何想法。

ps the API response is n array of strings. ps API 响应是 n 个字符串数组。

There are many way to pass informations from one component to another component:有很多方法可以将信息从一个组件传递到另一个组件:

  • If they are parent child you can pass it using @Input and @Output (it depends on the direction you need to use)如果他们是父子,您可以使用 @Input 和 @Output 传递它(这取决于您需要使用的方向)
  • If they are in different pages you can pass it in various ways using the routing (route parameter, query string, url fragment, state object)如果它们在不同的页面中,您可以使用路由以各种方式传递它(路由参数、查询字符串、url 片段、状态对象)
  • You can save the information in a service您可以将信息保存在服务中
  • You can use a Subject to which the first component will next the object and the second component is subscribed to get the updates您可以使用一个主题,第一个组件将在对象旁边,第二个组件被订阅以获取更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM