簡體   English   中英

使用Webpack 2將數據傳遞到ejs局部

[英]Pass data to ejs partials using Webpack 2

我正在使用Webpack 2和ejs-template-loader,我可以為頁面獲取數據,但是我需要將部分數據傳遞給partials之一。

<% 
var data = htmlWebpackPlugin.options.data;
var carouselElement = data.carouselElement;
if (data) {  %>
<!-- Header -->
<%- include ../../components/avalon/header/header.ejs %>
    <div class="page-mask">
        <div class="main-content <%= carouselElement%>">
            <%- include ../../components/hero.image/hero.image.ejs %>
            <%- include ../../components/content.carousel/content.carousel.ejs {carouselElement} %>
            <%- include ../../components/experiences/experiences.ejs %>
        </div>
    </div>
<%}%>

上面的代碼給了我這樣的錯誤

ENOENT:沒有這樣的文件或目錄

您知道如何將數據傳遞到局部嗎?

使用include在EJS的部分中傳遞數據:

<%- include( '../mypage', { data: "mydata" }); %>

mypage.ejs顯示

<%= data %> // will show mydata

使用EJS加載程序,似乎是這樣的:

var mainPage = require("ejs!./mainPage.ejs");
var subPage = require("ejs!./subPage.ejs");

var renderedPage = _.mainPage('<%= mainPage %>', { title: 'data', subPage:subPage  });

<%= renderedPage %>

mainPage.ejs

<main>
    <h1><%= title %></h1>
    <%= subPage({ title:'sub page', content: 'This is the subPage' }) %>
</main>

subPage.ejs

<div>
    <h2><%= title %></h2>
    <p><%= content %></p>
</div>

不要忘記在您的webpack conf文件中添加lodash插件:

plugins: [
    new webpack.ProvidePlugin({
        _: "underscore"
    })
]

請享用 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM