12

我正在尝试将标题传递给 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
    }
};

这是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>

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

4

4 回答 4

11

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

于 2017-06-11T01:09:00.300 回答
2

此处已报告此问题标题不起作用。#176

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

于 2017-04-27T12:08:10.677 回答
0

在 Webpack 中插入此配置

{
    test: /\.(index.html)$/,
    use: [
      {loader: "file-loader"},
      { loader: "extract-loader" },
      {
      loader: 'html-loader',
      options: {
        attrs: [':data-src']
      }
    }]
  }

确保此配置:

new HtmlWebpackPlugin({
  title: "My Page TItle",
  template: './index.html'
}),
于 2020-01-08T16:44:04.697 回答
-3

您是否尝试在模板 html 文件中注入标题标签./src/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">
    <title>Hello</title>
</head>
<body>
    <ui-view></ui-view>
</body>
</html>
于 2019-06-20T04:06:55.710 回答