Currently, I am in the process of integrating Angular2 with Laravel by following the guidance provided in the Quick Start guide. To compile all my typescript files into a consolidated app.js
, I have set up elixir-typescript and executed gulp
, which resulted in successful compilation. However, upon visiting my page in the browser, I encountered the following error in the console:
Error loading http://localhost/app/app.component.js as
"./app.component" from http://localhost/app/app.js ng:16:49
This is the content of my app.component.ts
file:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>My First Laravel-Angular 2 App</h1>'
})
export class AppComponent { }
Here is the information within my main.ts
file:
///<reference path="../../../typings/index.d.ts"/>
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
The resulting output file (app.js
) appears as follows:
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var AppComponent = (function () {
function AppComponent() {
}
AppComponent = __decorate([
core_1.Component({
selector: 'my-app',
template: '<h1>My First Laravel-Angular 2 App</h1>'
}),
__metadata('design:paramtypes', [])
], AppComponent);
return AppComponent;
}());
exports.AppComponent = AppComponent;
"use strict";
///<reference path="../../../typings/index.d.ts"/>
var platform_browser_dynamic_1 = require('@angular/platform-browser-dynamic');
var app_component_1 = require('./app.component');
platform_browser_dynamic_1.bootstrap(app_component_1.AppComponent);
//# sourceMappingURL=app.js.map
By altering the second-to-last line from:
var app_component_1 = require('./app.component');
to:
var app_component_1 = require('./app.js');
I successfully resolved the issue and managed to render the component on the page. Nevertheless, I struggled to configure my typescript main.ts
file to compile accordingly. Whenever I attempted to import from './app.js'
, the compiler failed.
It appears that my gulpfile concatenates the compiled typescript files without effectively bundling them together. How can I update the configuration to bundle each component into one unified .js
file, enabling seamless access throughout my application?
I believe these lines in the tsconfig.json
are pertinent to the situation:
{
...,
"module": "commonjs",
"moduleResolution": "node",
...
}