gpt4 book ai didi

for-loop - EJS + htmlWebpackPlugin + ejs-compiled-loader。无法使用循环制作导航列表,获取 TypeErrors

转载 作者:行者123 更新时间:2023-12-04 14:23:08 27 4
gpt4 key购买 nike

我的目标是根据我传递给 htmlWebpackPlugin 的数据创建导航选项,我想使用 for 创建它环形。每次尝试时,我都会遇到类似 Cannot read property 'length' of undefined 的错误.另外,我使用 ejs-compiled-loader因为我需要使用 <%- include path/to/template %> .而且我不使用 express (如果没有其他方法,我会使用它)。

index.ejs:

<!-- index.ejs -->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>

<%- include app/templates/header %>

</body>
</html>

header.ejs:

<!-- header.ejs -->
<div class="header">

<div class="header-inner">

<div class="header-logo"><img src="../../assets/images/logo.png" alt="Logo"></div>

<div class="nav">
<ul>
<% for (var i = 0; i < htmlWebpackPlugin.options.navItems.length; i++) { %>

<li><a href="<%= htmlWebpackPlugin.options.navItems[i].href %>">
<%= htmlWebpackPlugin.options.navItems[i].title %>
</a></li>

<% } %>
</ul>
</div>

</div>

</div>

webpack.config.js:

module.exports = {
// ...
plugins: [
new webpack.DefinePlugin({
IS_DEV: IS_DEV
}),

new HtmlWebpackPlugin({
template: '!!ejs-compiled-loader!./index.ejs',
title: appHtmlTitle
}),

// HEADER
new HtmlWebpackPlugin({
template: path.join(__dirname, 'app/templates/header.ejs'),
// template: '!!ejs-compiled-loader!./app/templates/header.ejs' tried as well
navItems: [
{
href: './',
title: 'startseite'
},
{
href: './offers.html',
title: 'angebote'
},
{
href: './about.html',
title: '&uuml;ber uns'
},
{
href: './contact.html',
title: 'kontakt'
}
],
test: 'Test'
})
],
module: {
rules: [
// BABEL
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/,
options: {
compact: true
}
},

// STYLES
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: IS_DEV
}
},
]
},

// CSS / SASS
{
test: /\.scss/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: IS_DEV
}
},
{
loader: 'sass-loader',
options: {
sourceMap: IS_DEV,
includePaths: [dirAssets]
}
}
]
},

// IMAGES
{
test: /\.(jpe?g|png|gif)$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
}
]
}
};

我做错了什么?如果ejs-compiled-loader无法访问传递的参数,易于维护的替代方案是什么?附言这是我在这里问的第一个问题,请不要判断得太严格。提前致谢。

最佳答案

以下是问题作者Alex Naidovich的代码, edited into their question (该修订版中“已解决”下方的所有内容)并表明它解决了问题。它的版权归 2018 Alex Naidovich 所有,并已获得 CC BY-SA 4.0 许可。

导航.js:

module.exports = [
{
href: './',
title: 'Home'
}, {
href: './about.html',
title: 'About Us'
}, {
href: './contact.html',
title: 'Contact Us'
}
]

webpack.config.js:

const NAV = require('path/to/nav.js'); // without extension

module.exports = {

// ...

plugins: {

// ...

new HtmlWebpackPlugin({
template: '!!ejs-compiled-loader!./index.ejs',
filename: 'index.html',
title: appHtmlTitle,

NAV: NAV // <- added NAV into plugin

}),
}

// ...

};

index.ejs:

<!doctype html>
<!-- html and head tags stuff -->
<body>

<% var NAV = htmlWebpackPlugin.options.NAV %> <!-- added this -->
<%- include app/templates/header %>

</body>
</html>

header.ejs:

<div class="header">
<div class="header-inner">
<div class="nav">
<ul>

<!-- header.ejs successfully gets variable NAV from index.ejs -->
<% NAV.forEach(function(navItem) { %>

<li>
<a href="<%= navItem.href %>"> <%= navItem.title %> </a>
</li>
<% }) %>
</ul>
</div>
</div>
</div>

这绝对有效。

关于for-loop - EJS + htmlWebpackPlugin + ejs-compiled-loader。无法使用循环制作导航列表,获取 TypeErrors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51594104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com