webpack html网页包插件:如何将title等参数注入模板

92dk7w1h  于 2023-02-04  发布在  Webpack
关注(0)|答案(5)|浏览(256)

我尝试将title传递给html-webpack-plugin,但它根本不创建title标记:(
谁能告诉我问题出在哪里

网络包.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服务器标题是不是注入?

bxgwgixi

bxgwgixi1#

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

f2uvfpb9

f2uvfpb92#

此问题已在此处报告Title not working. #176
如果你想添加动态<title>标签,你应该使用像ejsjade,...这样的模板语言。

ttcibm8c

ttcibm8c3#

在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'
}),
1mrurvl1

1mrurvl14#

现在可以像这样使用templateParameters:

new HtmlWebpackPlugin({
  template: 'index.html',
  templateParameters: {
    title: 'My web app',
  },

在index.html文件头中:

<title><%= title %></title>
omjgkv6w

omjgkv6w5#

你试过在模板html文件./src/index.html中插入title标签吗?

<!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>

相关问题