且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在EJS文件[WEBPACK]中获取JSON内容

更新时间:2022-06-24 22:35:27

最后,我找到了合适的解决方案.对于将来会来这里的人们:

Finally, i've find a proper solution. For future people who will come here :

一切都在webpack.config.js

1)第一次我需要JSON文件:

1) In a first time i require my JSON file :

const productsJson = require('.src/components/products.json')

2)然后,我在插件数组中添加以下代码:

2) Then i add the following code in my plugins array :

new HtmlWebpackPlugin({
    template: './src/index.ejs',
    templateParameters: {
        'myJSON': productsJSON
    }
})

3)最后,在index.ejs中不需要文件,它已经由webpack.config.js加载.要显示JSON内容,您只需要执行以下行:

3) Finally, In the index.ejs isn't needed to require the file, it's already loaded by the webpack.config.js. To display the JSON content, you simply need to do the following line :

<%= myJson.anything_In_My_Json %>