Javascript 使用 html 文件作为 angular 2 组件的模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34742264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using html file as template for component in angular 2
提问by Ilja
I have following app structure
我有以下应用程序结构
app
home
home.html
home.ts
home.sass
app.component.ts
index.html
Essentially I want to divide my pages into separate folders that have style, .ts and .html files related to them
基本上我想将我的页面分成单独的文件夹,这些文件夹具有与它们相关的样式、.ts 和 .html 文件
however at the moment loading a template like this
但是目前正在加载这样的模板
import {Component} from 'angular2/core';
@Component({
selector: 'admin-app',
template: 'home/home.html'
})
export class AppComponent { }
literary loads "home/home.html" string as a template where as I want html contents of the home.html file.
文学将“home/home.html”字符串加载为模板,因为我想要 home.html 文件的 html 内容。
回答by Michele Ricciardi
I think that if you want to pass a template, you should use
我认为如果你想传递一个模板,你应该使用
templateUrl: 'home/home.html'
That should make an http request and load the html into your component.
这应该发出一个 http 请求并将 html 加载到您的组件中。
回答by Simon S.
You may want to consider using Webpack. In that case, you simply:
您可能需要考虑使用 Webpack。在这种情况下,您只需:
@Component({
selector: 'my-comp',
template: require('./mycomp.component.html')
})
I've found it to be a solid solution thus far since it will actually throw a compile time error if the URL is incorrect.
到目前为止,我发现它是一个可靠的解决方案,因为如果 URL 不正确,它实际上会引发编译时错误。