【问题标题】:Angular 6 : Cannot find name 'bootbox'Angular 6:找不到名称'bootbox'
【发布时间】:2018-05-28 18:57:45
【问题描述】:

我正在尝试在我的 Angular 应用程序中使用 bootbox。我按照answer 的说明进行操作。但是当我构建项目时,我得到了

错误 TS2304:找不到名称“bootbox”。

我的 package.json 文件

"dependencies": {
    "@angular/animations": "6.0.2",
    "@angular/common": "6.0.2",
    "@angular/compiler": "6.0.2",
    "@angular/core": "6.0.2",
    "@angular/forms": "6.0.2",
    "@angular/http": "6.0.2",
    "@angular/platform-browser": "6.0.2",
    "@angular/platform-browser-dynamic": "6.0.2",
    "@angular/router": "6.0.2",
    "ag-grid": "17.1.1",
    "ag-grid-angular": "17.1.0",
    "angular2-tree-component": "3.1.0",
    "bootbox": "^4.4.0",
    "bootstrap": "^3.3.7",
    "core-js": "2.5.4",
    "install": "^0.11.0",
    "jquery": "3.3.1",
    "mydatepicker": "2.6.3",
    "ng2-webstorage": "2.0.0",
    "primeng": "6.0.0-alpha.1",
    "rxjs": "6.0.0",
    "rxjs-compat": "6.1.0",
    "zone.js": "0.8.26"
},
"devDependencies": {
    "@angular-devkit/build-angular": "~0.6.3",
    "@angular/cli": "~6.0.3",
    "@angular/compiler-cli": "6.0.2",
    "@angular/language-service": "6.0.2",
    "@types/bootbox": "^4.4.32",
    "@types/bootstrap": "^4.1.0",
    "@types/core-js": "0.9.46",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.3.1",
    "@types/node": "8.9.5",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
}

我的 angular.json

"scripts": [
    "node_modules/core-js/client/shim.min.js",
    "src/assets/js/Chart.bundle.min.js",
    "node_modules/jquery/dist/jquery.js",
    "node_modules/bootstrap/dist/js/bootstrap.js",            
    "node_modules/bootbox/bootbox.js",
    "src/assets/js/custom.js"
]

我相信,我正确地遵循了说明,但无法找到我哪里出错了。

【问题讨论】:

    标签: angular typescript bootbox


    【解决方案1】:

    第 1 步: 使用 npm 安装 jquery、bootstrap 和 bootbox

    第 2 步:在您的 angular.json(angular-cli.json 用于 Angular scripts 部分下添加以下内容

    "node_modules/jquery/dist/jquery.js",
    "node_modules/bootstrap/dist/js/bootstrap.js",            
    "node_modules/bootbox/bootbox.js",  
    

    第 3 步:安装类型

    npm install @types/jquery --save
    npm install @types/bootbox --save
    

    第 4 步: 构建项目并检查构建是否成功。如果构建成功,则到此为止。

    第五步: 如果编译不成功,则在你的src文件夹下创建一个名为typings.d.ts的文件,添加以下内容并编译工程。这应该可以解决问题。

    declare var bootbox: any;
    

    【讨论】:

      【解决方案2】:

      尝试在您的组件中导入 bootbox 文件,例如 -

       import * as bootbox from 'bootbox/bootbox.js'
      

      【讨论】:

      • 感谢雷米亚的回答。我已经尝试过这种方法。我能够构建项目,但在运行时出现此错误$.fn.modal is not defined; please double check you have included the Bootstrap JavaScript library.
      • 我也以正确的顺序添加了脚本。 (Jquery、bootstrap 和 bootbox)
      • 有一次我注意到 bootbox 脚本在 vendor.js 中,而其他脚本在 script.js 文件中。
      • 在更改 cli.json 后尝试重新启动服务器
      • 我已经停止 (CTRL +C) 并开始 (ng serve) 很多项目,但都是徒劳的。有没有其他重启命令?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 2016-01-24
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多