无法绑定到,因为它不是选择器组件的已知 属性

Can't bind to since it isn't a known property of selector component

我想将变量从一个组件传递到另一个组件,我正在使用 @input

这是我的父组件:

@Component({
    selector: 'aze',
    templateUrl: './aze.component.html',
    styleUrls: [('./aze.component.scss')],
    providers: [PaginationConfig],
})

export class azeComponent implements OnInit {
    public hellovariable: number = 100;
}

这是父组件的模板:

<app-my-dialog [hellovariable]="hellovariable"></app-my-dialog>

这是我的子组件:

@Component({
  selector: 'app-my-dialog',
  templateUrl: './my-dialog.component.html',
  styleUrls: ['./my-dialog.component.css']
})

export class MyDialogComponent implements OnInit {
  @Input() hellovariable: number;
  constructor() { }

  ngOnInit() {
    console.log(hellovariable);
  }

这是子模板:

 <h3>Hello I am {{hellovariable}}<h3>

这是 app.module.ts:

@NgModule({
    declarations: [
        AppComponent,
        MyDialogComponent

    ],
    entryComponents: [
        MyDialogComponent
      ],
    imports: [
        BrowserModule,
        routing,
        NgbModule,
        BrowserAnimationsModule,
        ToastrModule.forRoot(),
        RichTextEditorAllModule,
        FullCalendarModule,
        NgMultiSelectDropDownModule.forRoot(),
        LeafletModule.forRoot(),
        NgxGalleryModule,
        HttpClientModule,
        MatDialogModule,
        ReactiveFormsModule
    ],

当我显示父组件模板时出现此错误:

Can't bind to 'hellovariable' since it isn't a known property of 'app-my-dialog'.

知道如何解决这个问题吗?

ok 这个错误是因为你需要在 azeComponent 的模块中导入 MyDialogComponent。

没什么可尝试的

首先确保你已经将 Input 导入到你的组件中

import { Component, Input } from '@angular/core'; 

然后在命名时遵循 Pascal 约定 class

export class azeComponent implements OnInit 

应该改为

export class AzeComponent implements OnInit 

然后将您的组件注册到您的模块中declarations

同样将 BrowserModule 导入到你的模块中,类似这样

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
    MyDialogComponent,
    AzeComponent   
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

大多数情况下,您会收到此错误是因为您忘记将组件声明到您的 app.module.ts 中或做错了

app.module.ts

import { YourSpecificComponent } from './components/measureUnit/measure-unit-monthly/measure-unit-monthly.component';

@NgModule({
declarations: [
    AppComponent,
    MessageComponent,
    DashboardComponent,
    .....,
    YourSpecificComponent, // declared here
}

your-specific.component.ts

@Component({
    selector: 'app-your-specific', // your selector
    templateUrl: './your-specific.component.html',
    styleUrls: [
        '../some.css'
    ]
})

export class YourSpecificComponent implements AfterContentInit {
.....