Reference Address: HTTPS://GITHUB.COM/KITTENCUP/ANGULAR2-AMA-CN/ISSUES/18
Objective:
templateUrlRepresents the template address that the component relies on when it is run in the browser, so templateUrl it is not possible to fill in ./xxx.html this path, and you need to consider it from the browser's path.
src index.html index.js // 可能是打包后生成的 components button button.ts button.html
When you open index.html up you think about what the button.html load path is,/components/button/button.html
So it button.ts templateUrl should be written for you./components/button/button.html
Thus, templateUrl the path should be calculated based on the path of the HTML file you are executing
Problem
if eachcomponentthe components within the foldertemplateUrlall need to write on/componentsis not very troublesome, of course, here the path is relatively short and simple, if the path is very long/a/b/c/d/e/f/componentsso you are not in each component to write so long address, and later changes to each file will be changed.
Solution Solutions
Angular 2 help you think of this problem, you can use package: or asset: placeholder, the default value of the 2 placeholder is /packages , when you set templateUrl:‘package:/button/button.html‘ , at run time this address will be replaced /packages/button/button.html , here we need to modify the default placeholder address, by resetting Tokenfor PACKAGE_ROOT_URL the provide
Import {Provide,package_root_url} from ' Angular2/core '/components '})]
/components/button/button.html
More complex issues
package:Or a asset: placeholder can solve the path that is loaded at the time of development, when the project is published to the formal environment, our static files should be on the CDN, and of course PACKAGE_ROOT_URL it will solve this path problem, but not flexible enough if I want to templateUrl http://cdn.template.kittencup.com load from, and styleUrl to http://cdn.style.kittencup.comload, then one cannot be PACKAGE_ROOT_URL solved by simply
Solution Solutions
Angular 2 source code in both templateUrl and styleUrl before loading will be processed through the UrlResolver object, the UrlResolver object is obtained through dependency injection, the source code in Src/compiler/url_resolver.ts, the default UrlResolver is based on PACKAGE_ROOT_URLvalue to replace package: or asset: placeholder, so we can reset UrlResolver the code as follows
One
@Component ({ "hz-stepbody", "dm_template.html"//note, cannot be used here/dm_template.html}) Class Stepbody {}
Import {urlresolver} from ' Angular2/compiler '; class Myurlresolver extends Urlresolver { Resolve (BASEURL: String, url:string): string { if (url.substr ( -4) = = = '. css ') { return Super.resolve (' http://cdn.style.kittencup.com ', URL); } Else if (Url.substr ( -5) = = = '. html ') { return super.resolve (' http://cdn.template.kittencup.com ') , URL); } return super.resolve (baseUrl, URL);} } Bootstrap (APP, [Provide (Urlresolver, {useclass:myurlresolver})]);
Two
@Component ({selector: "hz-stepbody" "mytemplate:dm_template.html"
Import {provide, package_root_url} from ' Angular2/core 'angular2/compiler '; class Myurlresolver extends Urlresolver { Resolve (baseurl:string, url:string): string { var resolvedurl = url; if (Url.substr (0, 6) = = "MyTemplate") {= Resolvedurl.replace ("MyTemplate:", "/template/gz/"); } Else { = super.resolve (baseUrl, URL); } return Resolvedurl;} }
How to solve the path problem of angular 2 templateurl and styleurl?