Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm just getting started with Angular2, and I've followed the Angular2 quickstart and the tutorial.

Some context... When the user clicks a link in the top navbar of my webapp it makes a server side request. The page returned to the browser is an Angular2 app (index.html). The user can click localhost the home app or click localhost/customer the customer app. I don't understand how Angular2, and more likely SystemJS, is treating the difference in URL when loading/importing modules.

My project structure

When I load the home page the angular code works as expected where the view is rendered. The home index.html has System.config 'packages' = '/home' and the System.import references 'home/boot'.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout">
<head>
    <title>Home</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootswatch/3.2.0/cerulean/bootstrap.min.css" rel="stylesheet" media="screen" />
    <script>
        System.config({
            packages: {
                // the browser url is http://localhost
                "/home": {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });
        // the browser url is http://localhost
        System.import('home/boot').then(null, console.error.bind(console));
    </script>
</head>
<body>
<div layout:fragment="content">
    <div>
        <p th:text="#{home.welcome(${name})}">Welcome Message</p>
    </div>
    <div>
        <!-- ANGULAR HOME APP -->
        <home>Loading...</home>
    </div>      
</div>
</body>
</html>

When I load the customer page the browser gets a 404 loading browser.js and core.js. It is system.src.js that cannot find localhost/customer/angular2/platform/browser.js or localhost/customer/angular2/core.js. These imports are defined in my boot.ts and customer.component.ts.

boot.ts

import {bootstrap}    from 'angular2/platform/browser'

and customer.component.ts

import {Component, View} from 'angular2/core';

This is where I am stuck and not sure what system.src.js is trying to do or why it isn't loading the modules.

404 Error Code

My customer index.html has System.config packages set as '/' which I'm not clear on but seemed necessary as the browser url is localhost/customer. How should the System.config 'packages' option for this scenario be set?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout">
<head>
    <title>Customer</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootswatch/3.2.0/cerulean/bootstrap.min.css" rel="stylesheet" media="screen" />
    <script>
        System.config({
            packages: {
                // the browser url is http://localhost/customer
                '/': {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });
        // the browser url is http://localhost/customer
        System.import('boot').then(null, console.error.bind(console));
    </script>
</head>
<body>
<div layout:fragment="content">
    <div th:object="${customer}">
        <p>First Name: <span th:text="*{firstName}">FirstName</span></p>
        <p>Last Name: <span th:text="*{lastName}">LastName</span></p>
    </div>
    <div>
        <!-- ANGULAR CUSTOMER APP -->
        <customer>Loading...</customer>         
    </div>      
</div>
</body>
</html>

If I change the System.config packages to 'customer' the browser gets a 404 loading my customer boot.js (the compiled customer boot.ts). This lead me down the road to a few other changes

Change customer index.html System.import('boot').then(...) to have .js on 'boot' so the browser can find it

    <script>
        System.config({
            packages: {
                // the browser url is http://localhost/customer
                "customer": {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });
        // the browser url is http://localhost/customer
        System.import('boot.js').then(null, console.error.bind(console));
    </script>

Change customer/boot.js to have .js on the name of my registered component './customer.component'.

System.register(['angular2/platform/browser', './customer.component.js'], function(exports_1) {
    ...
}

These changes result in the customer app working albeit with a typescript compiler error "cannot find module ./customer.component.js".

It doesn't seem right to hand edit the compiled boot.js file. Is there a way to compile the .ts files so the generated .js files explicitly reference .js files? This seems like I am barking up the wrong tree.

I feel there is something I am missing with the customer index.html and how one can configure the System.config options.

share|improve this question

I think I am missing some information here, but I had the same kind of problems. I had to fiddle with the System.config and tsconfig.json file. The compilerOptions: outDir sourceRoot in the tsconfig.json file enabled me to choose mine own typescript source and output directory. I had also exclude some files from the compile process.

{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,

"noEmitOnError": true,
"outDir": "./wwwroot/app",
"sourceRoot": "scripts",
"inlineSources": true
},

"exclude": [
"node_modules",
"wwwroot",
"typings/main",
"typings/main.d.ts"
]
}

In mine System.config I had to make mine own mappings for several typescript modules.

System.config({
defaultJSExtensions: true,
packages: {
    'app': {
        format: 'register',
        defaultExtension: 'js'
    },
},

map: {
    'rxjs': 'vendor/js/rxjs',
    'angular2': 'vendor/js/angular2'
}
});

Mine suggestions are:

  1. Also look at your tsconfig.json file to solve compile problems.

  2. With the help of the map option in the System.config you maybe can solve your runtime problems.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.