简体   繁体   English

在组件B内使用组件A是一个好习惯吗?

[英]Is a good habit to use a component A inside component B?

I want to create an instance of the component A in the component B, but I do not know if this is a good habit in Angular or it is a complete mess. 我想在组件B中创建组件A的实例,但是我不知道这在Angular中是一个好习惯还是一个完整的烂摊子。

  • Component A : Modal to ask if you are sure about delete a file. 组件A :询问您是否确定要删除文件的模态。 Exists a showModal attribute in ts file which trigger the modal (two-way binding) if this flag is setting as true. 如果此标志设置为true,则在ts文件中存在一个showModal属性,该属性会触发模式(双向绑定)。
  • Component B : Where the files are managed to delete. 组件B :管理删除文件的位置。 If component A return true will be delete . 如果组件A返回true,则将其删除

Yes sure you could do something like: 是的,您可以执行以下操作:

component a : 成分a

popup code will be here. 弹出代码将在这里。 when user click on button it will emit event to parent. 当用户单击按钮时,它将向父级发出事件。 create event emitter here or some other way to convey button click to component b. 在此处创建事件发射器,或通过其他方式将按钮单击传达给组件b。

component b : 成分b

on some button(delete) click popup will appear. 在某些按钮(删除)上,将显示弹出窗口。 when you get event from child component a then you can invoke function in which you can do file operation. 当您从子组件中获取事件时,可以调用其中可以执行文件操作的函数。 Once you receive data from component a you can close popup in same function of event invoker. 从组件a接收数据后,您可以在事件调用程序的同一功能中关闭弹出窗口。

Or 要么

you can create service which will manage communication between reusable component a and other caller component(currently component b). 您可以创建服务来管理可重用组件a和其他调用方组件(当前为组件b)之间的通信。 You need to subscribe service field in 1 component and in second component invoke service function which will change the value of subscribed field in caller component. 您需要在1个组件中订阅服务字段,然后在第二个组件中调用服务功能,这将更改调用方组件中已订阅字段的值。

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

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