简体   繁体   English

是否可以通过一些修改在另一个组件内使用组件

[英]Is it possible to use a component inside another component with some modification

I have a home component and inside the Home component I am using the login component. 我有一个home组件,在Home组件中我正在使用登录组件。

There is another component named My Account and I have to use the login component in my account page as well. 还有另一个名为“我的帐户”的组件,我也必须在帐户页面中使用登录组件。 But I cannot use this login component inside the my account component without adding 'current' class <div id="tab-3" class="log-tab-content current"> because its a popup and adding the current class makes the popup to show so it will be hidden if I dont add current class. 但我不能在我的帐户组件中使用此登录组件而不添加“当前”类<div id="tab-3" class="log-tab-content current">因为它的弹出窗口并添加当前类会弹出显示所以如果我不添加当前类,它将被隐藏。 In the home page you have to click to view the login popup (component), thats how it gets activated. 在主页中,您必须单击以查看登录弹出窗口(组件),这就是如何激活它。 How could I reuse the component in this case ? 在这种情况下,我如何重用组件?

<form [formGroup]="LoginForm" (ngSubmit)="onSubmit()">
    <div id="tab-3" class="log-tab-content">
      <div class="login-form">
        <div class="login-mid">            
              </div>
        <input type="text"/>
        </div>
        <button type="submit" class="log-button log-button1">Reset Password</button>
      </div>
    </div>
</form>

Yes, @Input() properties are meant for that. 是的, @Input()属性就是为了这个。 You can change the behavior of component based on the input values. 您可以根据输入值更改组件的行为。

  1. Set current class dynamically using ngClass based on some input property sent by parent. 根据父发送的某些input属性,使用ngClass动态设置current类。
  2. Send input to login comp from my-account like [showPopup]="true" . 将输入发送到my-account login comp,如[showPopup]="true"

Code: 码:

login comp ts 登录组合

class LoginComponent{
  @Input() showPopup: boolean = false;
}

login.html 的login.html

<form [formGroup]="LoginForm" (ngSubmit)="onSubmit()">
    <div id="tab-3" class="log-tab-content" [ngClass]="{'current':showPopup === true}">
      <div class="login-form">
        <div class="login-mid">            
              </div>
        <input type="text"/>
        </div>
        <button type="submit" class="log-button log-button1">Reset Password</button>
      </div>
    </div>
</form>

my-account 我的帐户

<login [showPopup]="true"></login>

I think you could just wrap the login component in some dialect component(material dialect for example) instead of using customer class in order to change the css. 我认为您可以将登录组件包装在某个方言组件(例如材料方言)中,而不是使用客户类来更改css。 Then you are free to reuse your component. 然后您可以自由地重用您的组件。

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

相关问题 如何在另一个内部使用组件 - How to use a component inside another 是否可以将指令应用于另一个组件模板中的组件? - Is it possible to apply a directive to a component inside another component template? 是否可以在类位于Angular 6组件内部的应用程序组件上使用[ngClass]? - Is it possible to use [ngClass] on an app component where the class is inside the component for Angular 6? 是否可以使用角度为5的另一个组件中的某个组件的全局变量? - Is it possible to use a global variable from a component in another component in angular 5? 如何在离子中的另一个组件中使用一个组件? - How to use one component inside another component in ionic? 我可以在Angular的另一个组件内部使用组件吗? - Can I use component inside another component in Angular? 在另一个组件中使用一个组件,该组件在Angular 6中不起作用 - Use a component inside another component same module not working in Angular 6 如何在模型中使用子组件在Angular 6中打开另一个组件? - How to use Child Component inside the model to open an Another Component in Angular 6? 从另一个组件内的组件角度使用html模板 - Angular use html template from a component inside another component 另一个组件内的组件模板 - Component template inside another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM