Webpack: using require for ng-include

前端 未结 4 1001

I\'m new to webpack and right now I\'m using it for the first time in one of my angular projects.

I want to use the require function in my html file in order to requ

4条回答
  •  没有蜡笔的小新
    2020-12-14 16:41

    I've already posted this on https://stackoverflow.com/a/34815472/833093 but:

    To enable you must to configure the "relativeTo" parameter, otherwise your template partials get loaded at "/home/username/path/to/project/path/to/template/" (Check your bundle.js you're probably leaking your username in your projects)

    var ngTemplateLoader = (
        'ngtemplate?relativeTo=' + path.resolve(__dirname, './src/') +
        '!html'
    );
    
    module.exports = {
        ...
        module: {
            loaders: [
                {test: /\.html$/, loader: ngTemplateLoader},
            ],
        },
        ...
    }
    

    Then in your code, do a side-effect only require:

    // src/webapp/admin/js/foo.js
    require('../../templates/path/to/template.html');
    

    Then you can load the html:

提交回复
热议问题