[Angular 2] Component relative paths
Oingial aritial --> Link
Take away:
import { Component, OnInit } from '@angular/core';
@Component({
selector : 'contacts-header',
templateUrl: './header.component.html',
styleUrls : ['./header.component.css']
})
export class HeaderComponent implements OnInit {
}
When you use `templateUrl` & `styleUrls`, the path are relative to the application root.
So if you compoennt is put inside /src/app/header. Then way `templateUrl: './header.component.html'` is refer to 'src/header.component.html', so will report 404 error.
To way to solve the problem is introduce ´moudleId: moudle.id`.
CommonJS way:
import { Component, OnInit } from '@angular/core';
@Component({
moduleId: module.id, // fully resolved filename; defined at module load time
selector: 'contacts-header',
templateUrl: 'header.component.html',
styleUrls: ['header.component.css']
})
export class HeaderComponent implements OnInit {
}
//tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5"
}
}
SystemJS:
import { Component, OnInit } from '@angular/core';
@Component({
moduleId: __moduleName, // fully resolved filename; defined at module load time
selector: 'contacts-header',
templateUrl: 'header.component.html',
styleUrls: ['header.component.css']
})
export class HeaderComponent implements OnInit {
}
JSPM:
// If we decide to use JSPM, we use the typescriptOptions configuration format in the config.js file:
SystemJS.config({
typescriptOptions: {
module: "commonjs",
emitDecoratorMetadata: true,
experimentalDecorators: true
},
transpiler: false,
baseURL: "/dist",
map: {
app: 'src',
typescript: 'node_modules/typescript/lib/typescript.js',
angular2: 'node_modules/angular2',
rxjs: 'node_modules/rxjs'
},
packages: {
app: {
defaultExtension: 'ts',
main: 'app.ts'
},
angular2: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
Webpack:
// require
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: require('./header.component.html'),
styles: [require('./header.component.css')]
})
export class HeaderComponent implements OnInit {
}
or
// import
import { Component } from '@angular/core';
import { Component } from '@angular/core';
import headerTemplate from './header.component.html';
import headerStyle from './header.component.css';
@Component({
selector : 'my-app',
template : headerTemplate,
styles : [headerStyle]
})
export class HeaderComponent implements OnInit {
}
[Angular 2] Component relative paths的更多相关文章
- angular 引入 component 报错
每天抽出一些时间学习 Angular2 ,并准备把手头上的项目移植为 Angular2 , 不过今天又遇到了一些小问题. 准备写一个导航类适于管理后台常见的右边导航,如博客园的这种: ! 使用 g g ...
- [Angular] Test component template
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- [Unit Testing] Angular Test component with required
export default (ngModule) => { describe('Table Item component', () => { let $compile, directiv ...
- [Angular] Dynamic component rendering by using *ngComponentOutlet
Let's say you want to rending some component based on condition, for example a Tabs component. Insid ...
- [Angular] Dynamic component's instance and sorting
After create a component dynamic, we are able to change the component's props and listen to its even ...
- 在 Angular 2 Component 中使用第三方 JS 库
本文所有内容以 Angular 2 Quick Start 项目为基础,使用 TypeScript 语言. 如上图,最近遇到一个需求,需要在一个刚启动的 Angular 2 项目中使用 snap.sv ...
- [Angular] Change component default template (ng-content, ng-template, ngTemplateOutlet, TemplateRef)
Here is the defulat tab header template: <ng-template #defaultTabHeader let-tabs="tabsX" ...
- [Angular 2] Building a Toggle Button Component
This lesson shows you how to build a Toggle Button in Angular 2 from scratch. It covers using transc ...
- [Angular & Unit Testing] Testing a RouterOutlet component
The way to test router componet needs a little bit setup, first we need to create a "router-stu ...
随机推荐
- Js处理数据——前端分页工具
这几天有小伙伴讨论起了分页的相关问题,这里我也简单讲下前端如何简单便捷的利用Js(库)写出优雅,好用的分页工具. 分页是个很简单又超多接触的技术点,粗略来讲分如下两种: 真分页--每次根据页码.页大小 ...
- UISearchBar -- 备忘
搜索功能的备忘 UISearchBar UISearchBar是一个搜索栏,继承自UIView,也是常用的控件之一,所以特别写一篇备忘方便以后做工具文章. 例子: let searchBar = UI ...
- bzoj 1068: [SCOI2007]压缩 DP
1068: [SCOI2007]压缩 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 496 Solved: 315[Submit][Status] D ...
- 今天把登陆注册给改成tab了
真是解决了一个心头大患,本来以为必须请外包公司的工程师才做,自己研究了下也给解决了. 多亏去年做原型时学习了smarty.css.html这些最基本的网站前端开发的技术.FTP上传下载.linux的基 ...
- http://www.cnblogs.com/xdp-gacl/p/4040019.html
http://www.cnblogs.com/xdp-gacl/p/4040019.html
- 李洪强漫谈iOS开发[C语言-034]-程序的结构
- C# 的 WCF文章 消息契约(Message Contract)在流(Stream )传输大文件中的应用
我也遇到同样问题,所以抄下做MARK http://www.cnblogs.com/lmjq/archive/2011/07/19/2110319.html 刚做完一个binding为netTcpBi ...
- java.math.BigInteger使用心得总结(转)
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换.都会变成0.参考书籍发现使用使用BigInteg ...
- ajax 初始化请求前携带参数
$(function () { function SetAjax(wxOpenId, departCode) { $.ajaxSetup({ xhrF ...
- java学习多线程之线程状态