简体   繁体   中英

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:

$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).

Is it really better (performance-wize) and if so - why?

It looks like template: require('localFile.html') relies on RequireJS to load templates; templateUrl: '/path/to/localFile.html' is a built-in way to load a template.

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; otherwise, you can use built-in Angular functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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