简体   繁体   English

将一个组件的点击事件绑定到另一个组件

[英]Bind click event from one component to another one

Is it possible in Angular to dynamically bind a click event from a component to an existing component on the page.是否可以在 Angular 中将组件的点击事件动态绑定到页面上的现有组件。 To illustrate, I include this screenshot .为了说明,我附上了这个截图

The app consists of 4 simple components now;该应用程序现在由 4 个简单的组件组成; One shell component that defines the global layout, in the shell component I have a header component (blue outline) and a navigation component (red outline).一个定义全局布局的 shell 组件,在 shell 组件中我有一个标题组件(蓝色轮廓)和一个导航组件(红色轮廓)。 The green component is the component loaded by the current route.绿色组件是当前路由加载的组件。

I'm updating the navigation component using a simple service, like this:我正在使用一个简单的服务更新导航组件,如下所示:

@Injectable({ providedIn: 'root' })
export class NavigationService {

  private titleSource = new Subject<string>();
  private actionsSource = new Subject<ActionButton[]>();
  private menuItemsSource = new Subject<MenuItem[]>();

  title = this.titleSource.asObservable();
  actions = this.actionsSource.asObservable();
  menuItems = this.menuItemsSource.asObservable();

  constructor() {
  }

  setTitle(title: string) {
    this.titleSource.next(title);
  }

  setActions(actions: ActionButton[]) {
    this.actionsSource.next(actions);
  }

  setMenuItems(menuItems: MenuItem[]) {
    this.menuItemsSource.next(menuItems);
  }
}

The navgiation component simply uses this service like this:导航组件只是像这样使用这个服务:

@Component({
  selector: 'app-navigation',
  templateUrl: './navigation.component.html'
})
export class NavigationComponent implements OnInit {
  title: string = "";
  actions: ActionButton[] = [];
  menuItems: MenuItem[] = [];

  constructor(private navigation: NavigationService)  { 
    navigation.title.subscribe((title) => this.title = title);
    navigation.actions.subscribe((actions) => this.actions = actions);
    navigation.menuItems.subscribe((menuItems) => this.menuItems = menuItems);
  }
}

And displays it like this:并像这样显示它:

<div class="navigation">
    <div *ngIf="title.length" class="navigation__title">
        <h1>{{title}}</h1>
    </div>
    <div class="navigation__menu">
        <ul>
            <li *ngFor="let menuItem of menuItems"  [routerLinkActive]="['active']">
                <a routerLink="{{menuItem.path}}">{{menuItem.title}}</a>
            </li>
        </ul>
    </div>
    <div class="navigation__actions">
      <a *ngFor="let actionButton of actions" class="btn btn--primary">{{actionButton.title}}</a>
    </div>
</div>

Then in the currently activated component (the green one), I set the title and items like this然后在当前激活的组件(绿色的)中,我设置了这样的标题和项目

 ....

  constructor(private employeeService: EmployeeService, private navigation: NavigationService, private drawerService: NzDrawerService) { 
    navigation.setTitle('');
    navigation.setActions([
      {
        path: '',
        title: 'Add Employee'
      }
    ]);
    navigation.setMenuItems([
      {
        path: '/employees',
        title: 'Employees'
      },
      {
        path: '/teams',
        title: 'Teams'
      }
    ]);
  }
  ....

In this same active component I also have this method defined:在同一个活动组件中,我也定义了这个方法:

  createEmployee() {
    const drawer = this.drawerService.create<CreateEmployeeComponent>({
      nzContent: CreateEmployeeComponent,
      nzClosable: false,
      nzWidth: '33%',
    });

    drawer.afterClose.subscribe(data => {
      this.loadEmployees();
    });
  }

Now my question is, if it's somehow possible to call this method when I click the button rendered on my navigation component.现在我的问题是,当我单击导航组件上呈现的按钮时,是否可以以某种方式调用此方法。 And also set it dynamically from the currently activated component like I'm already doing for the title and navigation items并且还从当前激活的组件动态设置它,就像我已经为标题和导航项所做的那样

You can use Object orientation for this您可以为此使用Object orientation

Create a base-class创建基类

export class EmployeeCreateClass {
 createEmployee() {
    const drawer = this.drawerService.create<CreateEmployeeComponent>({
      nzContent: CreateEmployeeComponent,
      nzClosable: false,
      nzWidth: '33%',
    });

    drawer.afterClose.subscribe(data => {
      this.loadEmployees();
    });
  }
}

Make Navigation extend it:使Navigation扩展它:

export class NavigationComponent extends EmployeeCreateClass implements OnInit {
...

The navigation HTML:导航 HTML:

<button (click)="createEmployee()"></button>

Same thing with the components:组件也一样:

export class CurrentComponent extends EmployeeCreateClass implements OnInit {
....

The HTML: HTML:

<button (click)="createEmployee()"></button>

======================================================== Sorry I misunderstood the question, you can probably refine it further but maybe something like this: ================================================== ====== 对不起,我误解了这个问题,你可以进一步完善它,但可能是这样的:

@Injectable({ providedIn: 'root' })
export class NavigationService {

  private titleSource = new Subject<string>();
  private actionsSource = new Subject<ActionButton[]>();
  private menuItemsSource = new Subject<MenuItem[]>();
  private actionItems = [];

  title = this.titleSource.asObservable();
  actions = this.actionsSource.asObservable();
  menuItems = this.menuItemsSource.asObservable();

  constructor() {
  }

  setTitle(title: string) {
    this.titleSource.next(title);
  }

  setActions(actions: ActionButton[]) {
   this.actionItems = [];
    for(let i = 0; i < actions.length; i++) {
      this.actionItems.push(new Subject<void>());
    }
    this.actionsSource.next(actions);
  }

  setMenuItems(menuItems: MenuItem[]) {
    this.menuItemsSource.next(menuItems);
  }
}

Then in the HTML of navigation:然后在导航的 HTML 中:

      <a *ngFor="let actionButton of actions; let i = index" class="btn btn--primary" (click)="actionButtonClicked(i)">{{actionButton.title}}</a>

Then in the TS of the navigation:然后在导航的TS中:

  actionButtonClicked(i: number) {
    this.navigation.actionItems[i].next();
  }

Then in your component:然后在您的组件中:

navigation.setActions([
      {
        path: '',
        title: 'Add Employee'
      }
    ]);
// the bottom should get triggered every time actionButton gets `next`ed.
navigation.actionItems[0].subscribe(_ => { this.createEmployees(); });

Keep in mind this is me going at it at one go without an IDE but hopefully it helps you.请记住,这是我在没有 IDE 的情况下一次性完成的,但希望它对您有所帮助。

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

相关问题 将单击事件从一个子组件传递到另一个子组件 - Pass click event from one child component to another 在Angular中另一个组件的单击事件上刷新一个组件 - Refresh one Component on click event of Another Component in Angular (Angular) 事件从一个组件到另一个组件 - (Angular) event from one component to another 从一个组件到另一个组件的运行/发射器事件 - Run / emitter event from one component to another 是否可以在没有单击事件及其方法的情况下使用服务将数据从一个组件传递到另一个组件? - is it possible to pass data from one component to another using service without click event and it's method? 如何用户将元素 ID 从一个组件绑定到另一个组件 - How to user bind element ID from one component to another Angular 在按钮单击时从一个组件移动到另一个组件 - Angular moving from one component to another on button click 将单击按钮中的数据从一个组件传递到Angular中的另一个组件 - Passing data on button click from one component to another in Angular 如何从具有多个组件的 Angular 中的一个单击事件获得不同的输出? - How to get different output from one click event in Angular with more than one component? 如何在一个文件中编写和使用所有click事件并使用另一个组件Angular 5 - How can I write and use all click event in one file and use another component Angular 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM