如何将 Fomantic-UI 导入 Angular 项目

sma*_*use 5 jquery semantic-ui angular fomantic-ui

我正在开发一个基于 Fomantic-UI 框架(即 Semantic-UI 的一个分支)的 Angular 项目。

我已经安装了它:

npm install --save fomantic-ui

然后我在angular.json文件中添加了以下几行:

"styles": [
    "node_modules/fomantic-ui/dist/semantic.min.css",
    "src/styles.scss"
],
"scripts": [
    "node_modules/jquery/dist/jquery.min.js",
    "node_modules/fomantic-ui/dist/semantic.min.js"
]
Run Code Online (Sandbox Code Playgroud)

我还为它安装了类型npm install --save @types/semantic-ui,根据这个,它们应该可以与 Fomantic-UI 一起正常工作。

无论如何,这似乎不足以使用诸如modal(), dropdown(), 之类的函数calendar(),实际上我在 IDE (Webstorm) 和编译过程中都会出错:

TS2339: Property 'modal' does not exist on type 'JQuery<HTMLElement>'.

TS2339: Property 'calendar' does not exist on type 'JQuery<any>'.

TS2339: Property 'dropdown' does not exist on type 'JQuery<HTMLElement>'.

等等...

你知道在我的项目中导入 Fomantic-UI 的正确方法是什么吗?

Lin*_* Vu 0

对于 Typescript 类型,您需要在文件中添加一些行,tsconfig.json以向 Angular 应用程序声明这些类型。

{
  "compilerOptions": {
    ...
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types": [
      "hammerjs",
      "jasmine",
      "semantic-ui"
    ],
    ...
}
Run Code Online (Sandbox Code Playgroud)

还有tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [
      "semantic-ui"
    ]
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

测试文件中相同tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "types": [
      "jasmine",
      "node",
      "semantic-ui"
    ]
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)