简体   繁体   English

将Angular.JS模板与Webpack 4捆绑在一起

[英]Bundle Angular.JS templates with Webpack 4

I have a hybrid project with both AngularJS 1.x and Angular 6 code. 我有一个包含AngularJS 1.x和Angular 6代码的混合项目。 I'm trying to bundle all the .html templates to a js file so that all the following AngularJS calls that are done in *.js files - 我正在尝试将所有.html模板捆绑到js文件中,以便在* .js文件中完成以下所有AngularJS调用-

.directive('myDirective', [
    function () {
      return {
        restrict: 'E',
        templateUrl: '/components/.../derp/derp.html',

        ...
}}]);

will work. 将工作。

I tried using ngtemplate-loader with the following rule: 我尝试将ngtemplate-loader使用以下规则:

{
    test: /\/(components|views)\/.*?\.html$/,
    use: [
      {
        loader: 'ngtemplate-loader'
      },
      {
        loader: 'html-loader'
      }
    ]
}

and no cigar.. as I understand it, it'll only work for require(..) calls and not for plain string urls.. I didn't find any solution that works with js files that don't support require . 而且没有雪茄..据我了解,它仅适用于require(..)调用,不适用于纯字符串url ..我没有找到任何可用于不支持require js文件的解决方案。

Thanks 谢谢

you can import the html file in your js file. 您可以将html文件导入js文件。 and then try to use the reference. 然后尝试使用参考。

 var template = require('/components/.../derp/derp.html')   
 app.directive('myDirective', [
    function () {
      return {
        restrict: 'E',
        template: template,

        ...
}}]);

and instead of templateUrl use template only. 而不是templateUrl使用template而已。

what solved the issue for me was to convert all my states from 对我来说解决问题的是将我所有的州从

.state('...', {
    url: '...'
    templateUrl: './a/b/c.html',
    controller: '...'
})

to

.state('...', {
    url: '...'
    templateUrl: require('./a/b/c.html'),
    controller: '...'
})

while using the following webpack rule 在使用以下webpack规则时

{
    test: /\.html$/,
    use: [
      {
        loader: 'ngtemplate-loader',
        options: {
          relativeTo: path.resolve(__dirname, "app")
        }
      },
      {
        loader: 'html-loader'
      }
    ],
    exclude: [
      /index\.html$/
    ]
}

and it worked! 而且有效! I didn't have to change any other templateUrl: ... statements throughout the code :) 我不必在整个代码中更改任何其他templateUrl: ...语句:)

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

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