简体   繁体   English

Angular + Webpack:为什么比templte:require(file.html)更好,而不是用templateUrl指定它

[英]Angular + Webpack: Why is better to templte:require(file.html) than to specify it with a templateUrl

I saw several tutorials that are using this kind of notion to load html templates with Angular, like: 我看到了一些使用这种概念通过Angular加载html模板的教程,例如:

$stateProvider.state('stateName', {
   url: 'someState',
   template: require('localFile.html')
}

instead of: 代替:

$stateProvider.state('stateName', {
   url: 'someState',
   templateUrl: '/path/to/localFile.html'
}

My guess was that the templateUrl make the page do an AJAX call to require this page (I really not sure why it needs to do it). 我的猜测是templateUrl使页面进行AJAX调用以需要此页面(我真的不确定为什么需要这样做)。

Is it really better (performance-wize) and if so - why? 它真的更好(性能优化)吗?如果是,为什么?

It looks like template: require('localFile.html') relies on RequireJS to load templates; 看起来像template: require('localFile.html')依靠RequireJS加载模板; templateUrl: '/path/to/localFile.html' is a built-in way to load a template. templateUrl: '/path/to/localFile.html'是加载模板的内置方法。

Both versions support lazy loading and caching, so my guess is that there is no significant difference — if you already have RequireJS in your Angular app, you may prefer to use it for loading templates; 这两个版本都支持延迟加载和缓存,因此我猜没有什么明显的区别-如果您的Angular应用程序中已经有RequireJS,则您可能更喜欢使用它来加载模板。 otherwise, you can use built-in Angular functionality. 否则,您可以使用内置的Angular功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM