gpt4 book ai didi

javascript - 标题和侧边栏组件 Angular 6 中的功能共享

转载 作者:行者123 更新时间:2023-12-01 01:06:19 25 4
gpt4 key购买 nike

我需要在侧边栏中添加侧边栏组件功能。

我的 header 组件

import { Component, OnInit, Input,ViewChild } from '@angular/core';
import { SidebarComponent } from '../sidebar/sidebar.component';

@ViewChild(SidebarComponent) SidebarComponent;


ngOnInit() {
this.SidebarComponent.testFunction();
}

侧边栏组件

testFunction() {
console.log('value');
}

出于理解目的,我添加了一个重要的代码块。当我使用上面的代码时错误说,

ERROR TypeError: Cannot read property 'testFunction' of undefined at HeaderComponent.push../src/app/layout/components/header/header.component.ts.HeaderComponent.ngOnInit (header.component.ts:57)

你能帮我解决这个问题吗?

最佳答案

在 subview 初始化后调用它。

  @ViewChild(SidebarComponent) sidebarComponent: SidebarComponent;

ngAfterViewInit() {
this.sidebarComponent.testFunction();
}

关于javascript - 标题和侧边栏组件 Angular 6 中的功能共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55605267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com