【问题标题】:'ng-select' is not a known element'ng-select' 不是已知元素
【发布时间】:2018-12-28 23:38:23
【问题描述】:

这是我的代码: 我想在我的表单上添加 https://github.com/ng-select/ng-select 多选标签输入。

components.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgSelectModule } from '@ng-select/ng-select';


@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    NgSelectModule,
   ]

})

export class ComponentsModule { }

我的 modaltest.component.html

  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <label class="control-label">
                        <strong>Gusti:</strong>
                    </label>
               [ERROR] --->             <ng-select [items]="testitems" [hideSelected]="true" multiple="true" bindLabel="text1"></ng-select> 
   </div>

我的错误是:无法绑定到“项目”,因为它不是“ng-select”的已知属性为什么?????

你能帮帮我吗? 非常感谢!

【问题讨论】:

  • 是否在您的 ComponentsModule 中声明了 modaltest.component?
  • @GianlucaParis No...
  • 组件在哪个模块中?
  • 您需要将NgSelectModule 导入您的根模块(在Angular CLI 项目中通常称为app.module.ts)||将NgSelectModule 导入到您声明modaltest.component.ts 的特定模块中。我建议将其导入根目录,以便整个应用程序都可以使用该包。

标签: angular jquery-select2 angular6


【解决方案1】:

你必须在声明ModalTestComponent的模块中导入NgSelectModule,或者,如果你的ComponentsModule是一个包装器,你应该在其中导入声明ModalTestComponent的模块;这样:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgSelectModule } from '@ng-select/ng-select';
// add this line 
import { ModuleThatDeclaresModalTest } from 'path/to/module'


@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    NgSelectModule,
    // and this line 
    ModuleThatDeclaresModalTest 
   ]

})

export class ComponentsModule { }

作为替代方案,正如@Narm 所说,您可以在您的AppModule 中导入NgSelectModule,以便所有子模块都可以使用它。

【讨论】:

  • 我在 angular 5 中遇到同样的错误。尝试将依赖项保留在 AppModule 中,它仍然不起作用
  • 我找到了解决方案,如果你使用角度路由,这个模块必须加载到 app-routing.module.ts 中。
【解决方案2】:

首先你应该执行以下命令:

npm install --save @ng-select/ng-select

更多详情click here


如果你想添加自己的模块,你应该使用以下模式

import { YourModule } from 'path/to/your/module/without/@/symbol'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多