Asp.net mvc 如何通过保持';模板URL';原样
Webpack通过在dist文件夹中生成“js”来编译“typescript”文件。 我发现webpack正在将所有“templateurl”更改为“template”,如下所示: 我的Typescript组件:Asp.net mvc 如何通过保持';模板URL';原样,asp.net-mvc,angular,webpack,Asp.net Mvc,Angular,Webpack,Webpack通过在dist文件夹中生成“js”来编译“typescript”文件。 我发现webpack正在将所有“templateurl”更改为“template”,如下所示: 我的Typescript组件: @Component({ selector: 'app', encapsulation: ViewEncapsulation.None, templateUrl: 'app.html', // <---------------------- directives: [Header
@Component({
selector: 'app',
encapsulation: ViewEncapsulation.None,
templateUrl: 'app.html', // <----------------------
directives: [HeaderComponent, SideBar],
})
@组件({
选择器:“应用程序”,
封装:视图封装。无,
templateUrl:'app.html',//Webpack没有这样做。
如果这样做了,您可能有一个加载程序可以这样做
您可能正在使用带有预配置网页包配置的初学者工具包。
我的最佳选择是您正在使用
在angular2 webpack starter中,有一个名为angular2 template loader的插件,用于将Component.templateUrl
转换为Component.template
,方法是将文本字符串(即:html的路径)更改为require语句
这个过程将所有html和样式的内联起来,因此它不仅适用于html模板,也适用于样式URL
要删除此功能,请在网页包配置文件中更改以下内容:
module: {
loaders: [
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader', 'angular2-template-loader'],
exclude: [/\.(spec|e2e)\.ts$/]
}
]
}
为此:
module: {
loaders: [
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader'],
exclude: [/\.(spec|e2e)\.ts$/]
}
]
}
也就是说,删除进行内联的加载程序(angular2模板加载程序)
如果您想深入了解,以下是angular2模板加载器的代码部分:
var newSource = source.replace(templateUrlRegex, function (match, url) {
// replace: templateUrl: './path/to/template.html'
// with: template: require('./path/to/template.html')
return "template:" + replaceStringsWithRequires(url);
})
.replace(stylesRegex, function (match, urls) {
// replace: stylesUrl: ['./foo.css', "./baz.css", "./index.component.css"]
// with: styles: [require('./foo.css'), require("./baz.css"), require("./index.component.css")]
return "styles:" + replaceStringsWithRequires(urls);
});
// Support for tests
if (this.callback) {
this.callback(null, newSource, sourcemap)
} else {
return newSource;
}
};
你可以在评论中看到,这正是你所面临的问题。
您还可以查看
应该注意的是,选择退出内联将导致性能下降。您的应用程序将必须执行更多http请求和HTML标记(模板)将保持原始形式。换句话说,您编写的HTML代码不会得到webpack提供的优化。我不建议选择退出此加载程序。
module: {
loaders: [
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader'],
exclude: [/\.(spec|e2e)\.ts$/]
}
]
}
var newSource = source.replace(templateUrlRegex, function (match, url) {
// replace: templateUrl: './path/to/template.html'
// with: template: require('./path/to/template.html')
return "template:" + replaceStringsWithRequires(url);
})
.replace(stylesRegex, function (match, urls) {
// replace: stylesUrl: ['./foo.css', "./baz.css", "./index.component.css"]
// with: styles: [require('./foo.css'), require("./baz.css"), require("./index.component.css")]
return "styles:" + replaceStringsWithRequires(urls);
});
// Support for tests
if (this.callback) {
this.callback(null, newSource, sourcemap)
} else {
return newSource;
}
};