typescript 错误:模板解析错误:无法绑定到“myProperty”,因为它不是“myComponent”的已知属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41714563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:10:39  来源:igfitidea点击:

Error: Template parse errors: Can't bind to 'myProperty' since it isn't a known property of 'myComponent'

angulartypescriptwebpack

提问by cpiock

After upgrading from Angular 2.1.0 to 2.4.3 and webpack from 2.1.0-beta.25 to 2.2.0-rc.4 I recive this error if i run my webapp (the build works fine without errors):

从 Angular 2.1.0 升级到 2.4.3 并将 webpack 从 2.1.0-beta.25 升级到 2.2.0-rc.4 后,如果我运行我的 webapp(构建工作正常,没有错误),我会收到此错误:

Error: Template parse errors: Can't bind to 'center' since it isn't a known property of 'placeFilter'. 

At the moment I have one single central model like this (yes I know :-) I will change it afterwards):

目前我有一个这样的中央模型(是的,我知道:-)我会在之后更改它):

    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule } from '@angular/forms';
    import { HttpModule } from '@angular/http';

    import { AppRoutingModule } from './app.routes';

    ...
    import {MapComponent} from './map/map/map.component';
   ...
    import {PlaceFilterComponent} from './map/shared/components/placeFilter.component';
    ...

    @NgModule({
        imports: [BrowserModule, AppRoutingModule, FormsModule, HttpModule],
        declarations: [...
            MapComponent,
           ...
            PlaceFilterComponent,
            ...
        ],
        bootstrap: [AppComponent]
    })

    export class AppModule { }

My MapComponent contains a sub component called PlaceFilterComponent:

我的 MapComponent 包含一个名为 PlaceFilterComponent 的子组件:

import { Component, OnInit, OnDestroy, Input, Output, NgZone  } from '@angular/core';
import { ActivatedRoute, Params, Router} from '@angular/router';
import {Subscription} from "rxjs";
import {TimerObservable} from "rxjs/observable/TimerObservable";

import services

import models

@Component({
    selector: 'emersy-content',
    templateUrl: './map.component.html',
    providers: [myServices]
})

export class MapComponent implements OnInit, OnDestroy {
...

    constructor(private placeService: PlaceService, private externalService: ExternalServiceService, private zone: NgZone, private route: ActivatedRoute, private router: Router) {
        ...

        });
    }

   ...
}

Here my MapComponentTemplate:

这是我的 MapComponentTemplate:

<div class="filterbox-map" *ngIf="!onlyMap">
    <place-filter (onSearchLocationChange)="searchLocationChange($event)" (onShowSideBarButtonClick)="showSideBarButtonClick()" [center]="center" [searchTerm]="searchTerm"></place-filter>
</div>

And my PlaceFilter Component is made like this:

我的 PlaceFilter 组件是这样制作的:

import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';

import {Center} from '../models/center'

declare var google: any;

@Component({
    selector: 'place-filter',
    templateUrl: './placeFilter.component.html'
})

export class PlaceFilterComponent {
...
    private _center: Center;    
    get center() {
        return this._center;
    }
    @Input() set center(center: Center) {
        this._center = center;
    }



    constructor() {

    } 
...
}

My folder structure is like this:

我的文件夹结构是这样的:

app
L app.module.ts
L map
 L map
 | L map.component.ts
 | L map.component.html
 L shared
  L components
    L placefilter.component.ts
    L placefilter.component.html

采纳答案by ani5ha

export class PlaceFilterComponent {
...
    private _center: Center;    
    get center() {
        return this._center;
    }
    // try like this 
    @Input() center:Center;



    constructor() {

    } 
...
}

回答by Sibeesh Venu

Sometimes we forgot to check some basic things, and this happened with me in my Angularapplication. I created a new component with the command ng g c audio-elementand I used them in another component.

有时我们忘记检查一些基本的东西,这发生在我的Angular应用程序中。我使用该命令创建了一个新组件,ng g c audio-element并在另一个组件中使用了它们。

<!--AUDIO-->
<div *ngIf="theWorksheetElement?.type === 7" class="fill">
    <audio-element [worksheetElement]="theWorksheetElement" [style.pointer-events]="isResizing ? 'none' : 'auto'"></audio-element>
</div>

And I was always getting the error as follows.

我总是收到如下错误。

ERROR Error: Uncaught (in promise): Error: Template parse errors: Can't bind to 'worksheetElement' since it isn't a known property of 'audio-element'. 1. If 'audio-element' is an Angular component and it has 'worksheetElement' input, then verify that it is part of this module. 2. If 'audio-element' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

错误错误:未捕获(承诺):错误:模板解析错误:无法绑定到“worksheetElement”,因为它不是“音频元素”的已知属性。1. 如果 'audio-element' 是一个 Angular 组件并且它有 'worksheetElement' 输入,那么验证它是这个模块的一部分。2. 如果'audio-element' 是一个Web 组件,则将'CUSTOM_ELEMENTS_SCHEMA' 添加到该组件的'@NgModule.schemas' 以抑制此消息。

I was sure that I added this component to my related module. Now comes the funny partmy selector was different in the component typescript file.

我确信我将此组件添加到我的相关模块中。现在有趣的部分是我的选择器在组件打字稿文件中有所不同。

@Component({
  selector: 'app-audio-element',
  templateUrl: './audio-element.component.html',
  styleUrls: ['./audio-element.component.scss']
})

I changed the app-audio-elementto audio-elementand then it started working as expected. Just sharing for the people who might be making the same problem.

我改变app-audio-elementaudio-element,然后开始按预期工作。只是分享给可能遇到同样问题的人。