【问题标题】:How to import Fomantic-UI into an Angular project如何将 Fomantic-UI 导入 Angular 项目
【发布时间】:2019-12-05 17:09:16
【问题描述】:

我正在开发一个基于 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"
]

我还使用npm install --save @types/semantic-ui 为它安装了类型,根据this,它们应该可以在 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 的正确方法是什么吗?

【问题讨论】:

  • 没人知道...

标签: jquery angular semantic-ui fomantic-ui


【解决方案1】:

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

{
  "compilerOptions": {
    ...
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types": [
      "hammerjs",
      "jasmine",
      "semantic-ui"
    ],
    ...
}

还有tsconfig.app.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [
      "semantic-ui"
    ]
  },
  ...
}

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

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "types": [
      "jasmine",
      "node",
      "semantic-ui"
    ]
  },
  ...
}

【讨论】:

  • 这不起作用。我得到同样的错误。 jQuery 的导入是 Semantic/Fomantic 的依赖项呢?也许它与导入语义/语音的正确方法有关?
  • 这很奇怪,因为当它真的需要 jquery 时,它应该已经下载了它。您可能需要检查一些依赖版本。也许最近依赖项发生了一些变化,但没有被覆盖。
  • 但值得一试下载 jquery 并尝试使其运行。
  • 是的,我已经安装了 jQuery 及其类型,然后在 main.ts 中添加了 import * as $ from 'jquery'; 以在整个应用程序中使用 $。但是使用dropdownmodalcalendar 和其他带有$ 的语义模块会给我这些错误......
  • 还是谢谢你 ;)
【解决方案2】:

有几个问题,先安装jquery

install jquery

npm install jquery — save

error TS2339: Property 'modal' does not exist on type 'JQuery'

根据用户@tucker

  1. 属性modal 不存在于类型JQuery 安装

    npm install -D @types/bootstrap

【讨论】:

猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 2018-09-27
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多