在 Angular 6 中使用 ng2-select2

Using ng2-select2 in Angular 6

我不明白如何在我的 Angular 6 应用程序中使用 https://www.npmjs.com/package/ng2-select2。我怀疑我没有正确安装先决条件 Select2,因为当我尝试加载我的应用程序时,当我尝试加载我的应用程序时,我在浏览器的开发人员控制台中收到此错误:

Uncaught Error: Unexpected module 'Select2Module' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation.

这是我安装 Select2 先决条件所做的工作。我使用了此处讨论的手动安装选项:https://select2.org/getting-started/installation#manual-installation,但没有按照说明进行操作:

Include the compiled files in your page:

<link href="path/to/select2.min.css" rel="stylesheet" />

<script src="path/to/select2.min.js">

我用这些行更新了我的 angular.json 文件:

        "styles": [
           ...              
           "select2-4.0.5/dist/css/select2.min.css"
        ],
        "scripts": [
          ...
          "select2-4.0.5/dist/js/select2.full.js"
        ]

我很确定我的路径是正确的,因为如果我使用了错误的路径...

npm install
ng serve ...

...我用来启动我的 ng 服务器的命令会抱怨它可能会抱怨他们无法对 select2.full.jsselect2.min.css 文件进行调整。

我还更新了 app.modules.ts 文件。我添加了这个导入。

import { Select2Module } from 'ng2-select2';

这是我的 @NgModule 声明:

Select2Module

Select2Module 应该添加到导入数组而不是声明数组。

导入Select2Module到@NgModule:

 imports: [
    Select2Module
  ]