简体   繁体   English

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

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

I want to pass a variable from one component to another and I'm using @input我想将一个变量从一个组件传递到另一个组件,并且我正在使用 @input

This is my parent component :这是我的父组件:

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

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

This is the template of the parent component:这是父组件的模板:

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

This is my child component :这是我的子组件:

@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);
  }

This is the child template:这是子模板:

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

And this is the app.module.ts:这是 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 ],

When I show my parent component template I get this error:当我显示我的父组件模板时,我收到此错误:

Can't bind to 'hellovariable' since it isn't a known property of 'app-my-dialog'.无法绑定到“hellovariable”,因为它不是“app-my-dialog”的已知属性。

Any idea on how to fix this?关于如何解决这个问题的任何想法?

Few thing to try很少尝试

First make sure you have import Input into your component首先确保您已将 Input 导入到您的组件中

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

Then follow pascal convention when naming class然后在命名类时遵循pascal约定

export class azeComponent implements OnInit 

should change to应该改为

export class AzeComponent implements OnInit 

Then register your component into your module declarations然后将您的组件注册到您的模块declarations

Also import BrowserModule into your module also something like this还将 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 { }

Mostly you'll get this error because you forgot to declare the component into your app.module.ts or did it wrong大多数情况下,您会收到此错误,因为您忘记将组件声明到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 {
.....

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

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

相关问题 无法绑定到“控件”,因为它不是(myComponent)的已知属性 - Can't bind to 'control' since it isn't a known property of (myComponent) 无法绑定到“ useStickyClasses”,因为它不是“ div”的已知属性 - Can't bind to 'useStickyClasses' since it isn't a known property of 'div' Angular 7:无法绑定到“元素”,因为它不是 - Angular 7 : Can't bind to 'element' since it isn't a known property of 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'input' 无法绑定到“matMenuTrigger”,因为它不是“a”的已知属性 - Can't bind to 'matMenuTrigger' since it isn't a known property of 'a' 无法绑定到“数据集”,因为它不是“画布”的已知属性 - Can't bind to 'datasets' since it isn't a known property of 'canvas' 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 无法绑定到“appIfRoles”,因为它不是“p”的已知属性 - Can't bind to 'appIfRoles' since it isn't a known property of 'p' 无法绑定到“ chartType”,因为它不是“ canvas”的已知属性 - Can't bind to 'chartType' since it isn't a known property of 'canvas' 错误:无法绑定到“ ngModel”,因为它不是的已知属性 - Error: Can't bind to 'ngModel' since it isn't a known property of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM