Haroon
Haroon

Reputation: 500

Angular 2 component .html path

I try to use relative paths for html in component also i am mapping my js files in dist folder.. my component code is

app/app.component.ts

import { Component } from '@angular/core';
@Component({
  moduleId: module.id,
  selector: 'my-app',
  templateUrl: 'app.component.html'
})
export class AppComponent { }

the html file is in same directory i.e app/app.component.html

<h1> Wolaa </h1>

I am getting following error

Error i get

Its totally clear that angular is finding app.component.html file at "dist/app/app.component.html" which is not there how can i fix this??

My systemjs.config.js file is

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function(global) {
  // map tells the System loader where to look for things
  var map = {
    'app':                        'dist', //'app',
    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs'
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
  };
  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'forms',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router',
    'router-deprecated',
    'upgrade',
  ];
  // Individual files (~300 requests):
  function packIndex(pkgName) {
    packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
  }
  // Bundled (~40 requests):
  function packUmd(pkgName) {
    packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
  }
  // Most environments should use UMD; some (Karma) need the individual index files
  var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
  // Add package entries for angular packages
  ngPackageNames.forEach(setPackageConfig);
  var config = {
    map: map,
    packages: packages
  };
  System.config(config);
})(this);

and my tsconfig.json is

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir": "dist"
  }
}

Upvotes: 4

Views: 1695

Answers (3)

mrgoos
mrgoos

Reputation: 1306

This is the best solution that I could find so far: moduleId: module.id.replace("/dist/", "/src/"); where src in my case is app. Taken from here.

Upvotes: 0

Noor Ahmed
Noor Ahmed

Reputation: 1617

Try placing a "./" before your relative path like:

import { Component } from '@angular/core';
@Component({
  moduleId: module.id,
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent { }

Upvotes: 0

Arif
Arif

Reputation: 1399

Here are some details about template relative paths in the documentation that may help:

https://angular.io/docs/ts/latest/cookbook/component-relative-paths.html

Upvotes: 1

Related Questions