primeng下拉组件错误('p-dropdown'不是已知元素)

Lui*_*ino 6 dropdown primeng angular

按照指南:https: //www.primefaces.org/primeng/我已经尝试安装PrimeNG与Angular4一起使用,按照上面详述的步骤,但是我收到错误:

'p-dropdown' is not a known element:
Run Code Online (Sandbox Code Playgroud)

我尝试重建项目,正如其他帖子所建议的那样,但它对我不起作用.任何提示?

以下是所有细节:

- PrimeNg安装

npm install primeng --save
Run Code Online (Sandbox Code Playgroud)

- file:testdropdown.component.html

<p-dropdown [options]="cities" [(ngModel)]="selectedCity"></p-dropdown>
Run Code Online (Sandbox Code Playgroud)

- file:testdropdown.component.ts

import { DropdownModule } from 'primeng/primeng';
import { Component, OnInit } from '@angular/core';
import { SelectItem } from 'primeng/primeng';

@Component({
  selector: 'app-testdropdown',
  templateUrl: './testdropdown.component.html',
  styleUrls: ['./testdropdown.component.css']
})
export class TestdropdownComponent implements OnInit {

  cities: SelectItem[];

  selectedCity: string;

  constructor() {

  this.cities = [];
    this.cities.push({ label: 'Select City', value: null });
    this.cities.push({ label: 'New York', value: { id: 1, name: 'New York', code: 'NY' } });
    this.cities.push({ label: 'Rome', value: { id: 2, name: 'Rome', code: 'RM' } });
  }

  ngOnInit() {
  }

}
Run Code Online (Sandbox Code Playgroud)

- 错误:

VM1128:185 [CodeLive] HTTP detected: Connecting using WS
zone.js:630 Unhandled Promise rejection: Template parse errors:
Can't bind to 'options' since it isn't a known property of 'p-dropdown'.
1. If 'p-dropdown' is an Angular component and it has 'options' input, then verify that it is part of this module.
2. If 'p-dropdown' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("<p-dropdown [ERROR ->][options]="cities" [(ngModel)]="selectedCity"></p-dropdown>
"): ng:///AppModule/TestdropdownComponent.html@0:12
'p-dropdown' is not a known element:
1. If 'p-dropdown' is an Angular component, then verify that it is part of this module.
2. If 'p-dropdown' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<p-dropdown [options]="cities" [(ngModel)]="selectedCity"></p-dropdown>
Run Code Online (Sandbox Code Playgroud)

Edu*_*gas 14

在声明组件的模块中导入下拉模块.

import {DropdownModule} from 'primeng/primeng';

@NgModule({
  imports: [
   DropdownModule
  ],
  declarations: [TestdropdownComponent ]

})
export class myModule { }
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,导入 DropdownModule 还不够,请检查下面的 @Karthik H 答案。 (2认同)

Kar*_*k H 5

如果此问题仍然存在,则可能必须再测试一件事,即是否导入了“ FormsModule”,如果未导入,请尝试,

import { FormsModule } from '@angular/forms';
import { DropdownModule } from 'primeng/primeng';

@NgModule({
  imports: [
    DropdownModule,
    FormsModule
  ],
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题。