html-webpack-plugin:如何将标题等参数注入模板

Mio*_*vic 12 webpack html-webpack-plugin

我试图将标题传递给 html-webpack-plugin,但它根本不创建标题标签:(

有人可以告诉我问题出在哪里吗

webpack.js

var HtmlWebpackPlugin = require('html-webpack-plugin');
var webpack = require('webpack');

module.exports = {
    entry: ['./src/app/main.ts'],
    output: {
        filename: 'build.js',
        path: 'dist'
    },
    resolve: {
        root: __dirname,
        extensions: ['', '.ts', '.js', '.json']
    },
    resolveLoader: {
        modulesDirectories: ["node_modules"]
    },
    devtool: "source-map",
    plugins: [
        new HtmlWebpackPlugin({
            title : 'Hello',
            template: './src/index.html',
            inject: 'body',
            hash: true,
        })
    ],
    module: {
        loaders: loaders
    }
};
Run Code Online (Sandbox Code Playgroud)

这是index.html

<!doctype html>
<html lang="en">
<head>
    <noscript>
        <meta http-equiv="refresh" content="0; url=https://en.wikipedia.org/wiki/JavaScript">
    </noscript>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
    <ui-view></ui-view>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我启动 webpack 服务器标题时没有注入?

Sam*_*nda 10

<title><%= htmlWebpackPlugin.options.title %></title>在你的 html 文件中试试这个。作为参考,您可以在我的 repos webpack-setup 中查看index.html文件。

  • 你能解释一下这是如何工作的吗?在构建时必须有某种模板引擎在某个地方运行来处理这个问题。 (2认同)

iar*_*oyo 3

此问题已在此处报告,标题不起作用。第176章

如果你想添加动态<title>标签,你应该使用模板语言,如ejs, jade, ...