【问题标题】:ng-bootstrap typeahead TypeError: Object(...) is not a functionng-bootstrap typeahead TypeError: Object(...) is not a function
【发布时间】:2018-09-23 19:19:13
【问题描述】:

我想在 angular5 应用程序中使用 angular bootstrap typeahead linkToTypeahead。 但我不断收到以下错误:

ERROR TypeError: Object(...) is not a function
at new NgbTypeahead (typeahead.js:52)
at createClass (core.js:12449)
at createDirectiveInstance (core.js:12284)
at createViewNodes (core.js:13742)
at callViewAction (core.js:14176)
at execComponentViewsAction (core.js:14085)
at createViewNodes (core.js:13770)
at callViewAction (core.js:14176)
at execComponentViewsAction (core.js:14085)
at createViewNodes (core.js:13770)

还有:

ERROR TypeError: Cannot read property 'instance' of undefined
at nodeValue (core.js:11753)
at Object.eval [as handleEvent] (NgbdTypeaheadBasic.html:9)
at handleEvent (core.js:13547)
at callWithDebugContext (core.js:15056)
at Object.debugHandleEvent [as handleEvent] (core.js:14643)
at dispatchEvent (core.js:9962)
at eval (core.js:10587)
at HTMLDocument.eval (platform-browser.js:2628)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4740)

我从link 网站复制了 Simple Typeahead 代码。

我的 package.json 文件:

{ "name": "test-type-ahead", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build --prod", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "^5.2.0", "@angular/common": "^5.2.0", "@angular/compiler": "^5.2.0", "@angular/core": "^5.2.0", "@angular/forms": "^5.2.0", "@angular/http": "^5.2.0", "@angular/platform-browser": "^5.2.0", "@angular/platform-browser-dynamic": "^5.2.0", "@angular/router": "^5.2.0", "@ng-bootstrap/ng-bootstrap": "^2.0.0-alpha.0", "bootstrap": "^4.1.0", "core-js": "^2.4.1", "rxjs": "^5.5.6", "zone.js": "^0.8.19" }, "devDependencies": { "@angular/cli": "~1.7.3", "@angular/compiler-cli": "^5.2.0", "@angular/language-service": "^5.2.0", "@types/jasmine": "~2.8.3", "@types/jasminewd2": "~2.0.2", "@types/node": "~6.0.60", "codelyzer": "^4.0.1", "jasmine-core": "~2.8.0", "jasmine-spec-reporter": "~4.2.1", "karma": "~2.0.0", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "^1.2.1", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "protractor": "~5.1.2", "ts-node": "~4.1.0", "tslint": "~5.9.1", "typescript": "~2.5.3" } } 到目前为止我所尝试的:

  1. 用 npm 重新安装 node_modules -- 没有帮助
  2. 使用 Angular CLI 开始了一个全新的项目,并在那里试用了代码 - 没有帮助。

非常感谢任何帮助!

【问题讨论】:

  • 2.0.0-alpha.0 是一个未记录的 alpha 版本,AFAIK 是与 Angular 6 一起使用的初步版本。使用 ng-bootstrap 的最新稳定版本:1.1.1。跨度>
  • 使用 ng-bootstrap :1.1 v 并且在导入 ng-bootstrap 的主模块时不要忘记在 app.module 中使用 .forRoot()

标签: angular typescript typeahead ng-bootstrap typeerror


【解决方案1】:

所以我的问题通过安装 ng-bootstrap 的稳定版本解决了:

所以 package.json 文件:

`dependencies
 ....
 "@ng-bootstrap/ng-bootstrap": "^1.1.1",
 ....
 dependencies`

感谢 JB Nizet 和 Niladri 的快速解答


编辑:另外,正如@Niladri 在 cmets 中所说,您需要将forRoot() 用于您要使用的模块。这是用于 Angular 5 的 official documentation of version 1.1,它在此处进行了说明,但新版本 Angular 的新文档并没有说明,因为它已更改。所以记住这一点!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 2019-04-05
    • 2019-01-30
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2021-10-26
    相关资源
    最近更新 更多