简体   繁体   English

Angular 2.2和ng-bootstrap无法读取未定义的属性“可观察”

[英]Angular 2.2 and ng-bootstrap Cannot read property 'Observable' of undefined

I've reported this issue on Github , but asking it here won't hurt either 我已经在Github上报告了此问题,但是在这里问也不会有任何问题

Bug description: 错误描述:

I added in ng-bootstrap and started getting this error. 我添加了ng-bootstrap并开始出现此错误。

VM325:9 Uncaught TypeError: Cannot read property 'Observable' of undefined(…)webpackUniversalModuleDefinition @ VM325:9(anonymous function) @ VM325:10module.exports @ scripts.bundle.js:28476 @ scripts.bundle.js:6__webpack_require__ @ inline.bundle.js:53826 @ scripts.bundle.js:37__webpack_require__ @ inline.bundle.js:53webpackJsonpCallback @ inline.bundle.js:24(anonymous function) @ scripts.bundle.js:1 main.bundle.js:420

Link to minimally-working plunker that reproduces the issue: 链接到重现此问题的最低工作插件:

Please see my GitHub branch . 请参阅我的GitHub分支

This commit does not have this error 4a049710ba900f5aef8d11141491607e45e49cc2. 此提交没有此错误4a049710ba900f5aef8d11141491607e45e49cc2。 This commit fcf8260c8b560f665fc4a34b49dd47f1700c3945 'bring in bootstrap - couldn't get js include without observable of undefined error' introduced the error. 此提交fcf8260c8b560f665fc4a34b49dd47f1700c3945 “自举-无法在未观察到未定义错误的情况下获取js include”引入了错误。

Version of Angular, ng-bootstrap, and Bootstrap: Angular,ng-bootstrap和Bootstrap的版本:

Angular 2.2 - CLI Angular 2.2-CLI

ng-bootstrap: "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.14", ng-bootstrap:“ @ ng-bootstrap / ng-bootstrap”:“ ^ 1.0.0-alpha.14”,

I just cloned your repo, you need to do 3 changes on different files, I'm going to explain each one. 我只是克隆了您的存储库,您需要对不同的文件进行3个更改,我将逐个解释。

package.json 的package.json

You forgot to add the bootstrap dependency, when I cloned your repo I got errors due to the missing package, just do: 您忘了添加引导程序依赖项,当我克隆您的存储库时,由于缺少软件包而出现错误,请执行以下操作:

npm i bootstrap@next --save

Or add "bootstrap": "^4.0.0-alpha.5" into your dependencies list. 或在您的依赖项列表中添加"bootstrap": "^4.0.0-alpha.5"

src/app/app.component.html SRC /应用程序/ app.component.html

You don't have a router-outlet on your main component, without that your routes will never work, change your app.component.html into this: 您的主要组件上没有路由器出口,如果没有,路由将永远无法工作,请将app.component.html更改为:

<h1>Energy with Angular 2.0</h1>

<router-outlet></router-outlet>

Your EnergyComponent will be injected inside the <router-outlet> by angular. 您的EnergyComponent将按角度注入<router-outlet>

angular-cli.json 角cli.json

Last but not least your main problem, the Observable error is due to the fact you are including ng-bootstrap on your scripts , just remove that line, it should look something like this: 最后但并非最不重要的一点是,Observable错误是由于您在scripts中包含ng-bootstrap而导致的,只需删除该行,它应该类似于以下内容:

"styles": [
  "styles.css",
  "../node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts": [],

Why? 为什么? Because you are already including ng-boostrap when you import it on your app.module.ts you only add scripts when they are an external dependencies, but you don't use them directly on your code. 因为当您在app.module.ts上导入ng-boostrap时,已经包含了它们, 因此仅在脚本是外部依赖项时才添加脚本,但是不会直接在代码中使用它们。 For example if you were using the pure boostrap way, css + javascript/jquery you would add the bootstrap.js on your scripts array 例如,如果您使用的是纯boostbosp方式,即CSS + javascript / jquery,则可以在脚本数组中添加bootstrap.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM