Check the update on the bottom!
I have a service that throws an error while the app is bootstrapping. Cannot read property 'call' of undefined
. I'm using ng2 2.4.2 and angular-cli 1.0.0-beta.24.
ERROR
Uncaught TypeError: Cannot read property 'call' of undefined at webpack_require (bootstrap 81b10f8…:52) at Object.621 (environment.ts:8) at webpack_require (bootstrap 81b10f8…:52) at Object.450 (src async:7) at webpack_require (bootstrap 81b10f8…:52) at Object.1057 (util.service.ts:35) at webpack_require (bootstrap 81b10f8…:52) at webpackJsonpCallback (bootstrap 81b10f8…:23) at main.bundle.js:1
As you can see it's a problem with the util service - this looks as follows:
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Project } from '../datatypes/';
import { Response } from '@angular/http';
@Injectable()
export class UtilService {
constructor(private router: Router) {}
public redirectToProject(project: Project) {
let query = project.ProjectName.split(' ')
.join('-')
.concat('-' + project.Id)
.toLowerCase();
this.router.navigate(['/project', query]);
}
public extractData(res: Response) {
let body = res.json();
return body || {};
}
}
Strange: When inspecting the source file in chrome it has no syntax highlighting, which would suggest a syntax error - in my opinion there's none though.
Update January 20. 2017
I updated to ng2.4.4 and angular-cli 1.0.0-beta26. The problem is still the same.
While playing around, Arjan found out that it works with beta 21. Will have to check the changes. The problem now isn't in the service above but in the environment.ts
file (which has all the defaults).
For me, this was caused by using our own client-side JavaScript to dynamically load the webpack-generated bundles. That was easily fixed in our own loader script.
When using webpack, a regular Angular 2
index.html
does not include any<style>
or<script>
elements, but just<app-root></app-root>
. When runningng serve
orng build
, the file is enhanced server-side to add something like the following to the end of the file:...or, for
ng build --prod
:The resulting server-side HTML file is sent to the browser. And browsers will run
<script>
elements already present in the HTML in the order in which they are encountered.But that's different when adding those
<script>
elements client-side, dynamically at runtime, which by default will be executed asynchronously. See the notes on "parser-inserted scripts" and "script-inserted scripts" on MDN's script page.In our case we're including the Angular 2 application into pages in our CMS. To avoid having to change the CMS page whenever we release a new version (which will change the hashes in the names of the generated bundles), we're adding the bundles using our own JavaScript code. For a browser these are "script-inserted scripts". To ensure these are executed in the correct order, just set
async
to false (settingdefer
did not do the trick for me):Or, when using
document.write
, ensure it includes theasync="false"
attribute:(Fun fact: beware that Chrome 55 and later might skip these blocking scripts on slow connections, if they are hosted on a different domain.)
Without this, things most often were fine up to angular-cli 1.0.0-beta.21, though we did see an occasional "Can't find variable: webpackJsonp". For later versions different errors might show, depending on which script is executed first, and which browser is used. Like:
Also note that
vendor.bundle.js
andpolyfills.bundle.js
were not used in older versions. And the scripts might need to be inserted somewhere below the<app-root>
element.I think it happens when you make changes to module imports during run time.
Run the application again using
ng serve
and it fixed the issue for me.It may also occur when you try to use components of lazy loaded modules in other modules before the lazy module getting loaded or such similar scenarios.
In my case, I needed to make sure that I had the script tags in the right order.
I had a similar issue. I updated to @angular-cli (1.0.0-rc.2) and @angular/core(2.4.9) and followed Sandrooco's solution. However, there is an additional file polyfills.bundle.js required to work.
I hope it helps.
Finally I found the answer - works with
beta.32.3
. We have to use these files:It's also important to update the local and the global package. Updating globally is described well on their github page. Updating locally is easy too - simply update the dependency in your package.json.
Important: Since
beta.29
angular cli is the package@angular/cli
, notangular-cli
anymore..