如何使用 webpack 编译 angular 模板?

How can you compile angular templates with webpack?

想法是在所有 html 个文件中有 1 个 JS 文件。 在通过 require:

使用它之后
templateUrl: require('./views/user-list.html')

能否分享一下您的经验? 我在谷歌上搜索它并找到了几个用于 webpack 的加载器,但不确定使用什么。

我决定将 ng-cache loader 与 ES2015 语法一起用于 webpack:这意味着 import from 而不是 require

我的部分 webpack 配置:

module: {
    loaders: [
      { test: /\.js$/, loader: 'babel', include: APP },
      { test: /\.html$/, loader: 'ng-cache?prefix=[dir]/[dir]' },
    ]
  },

以及带模板的指令示例:

import avatarTemplate from './views/avatar.html';

const avatar = function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      user: '='
    },
    template: avatarTemplate
  }
};

export default avatar;

你的回答是正确的。但是只是提供替代方案:

const avatar = function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      user: '='
    },
    template: require("../path/to/file.html"),
  }
};

export default avatar;

在 Webpack.config 文件中,您可以添加如下所示的主要 html

 plugins: [ 
new HtmlWebpackPlugin({
    template: 'index.html'
}),
new ExtractTextPlugin('bundle.css')

], 在 package.json 中包含 html-loader 并在配置块中单独使用以下方法就足够了。

$stateProvider.state('app', {
        url: '/app',
        template: require('html-loader!root/app/Common/templates/role.html'),
        controller: 'roleController'
    })

所以所有的部分都将被捆绑在 bundle.js itself.No 需要在 webpack-config 中添加加载器