如何将数组从节点传递到.js文件(pugJs) [英] How to pass a array from node to .js file (pugJs)

查看:151
本文介绍了如何将数组从节点传递到.js文件(pugJs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制图表(使用 ChartJS )作为快速响应。图表的数据点以数组形式存储在节点中。我必须将其传递给chartJS函数。

I am trying to render a chart (using ChartJS) as express response. The data points for the chart are stored in node as array. I have to pass it on to the chartJS function.

为此,我目前正在使用express的 res.render()方法。我设法将值传递到 .pug 文件。现在,我必须将数据点从 .pug 文件移动到 .js 文件/ ChartJS函数。

For that I'm currently using express's res.render() method. I have managed to pass the values to .pug file. Now i have to move the data points from .pug file to the .js file/ChartJS function.

我该如何完成?这是正确的方法吗?

How do i accomplish this? is this is the correct approach?

我希望最终结果是存储在 node 中的数组值必须会反映在具有ChartJS功能的 .js 文件中。

The end result I expect is that the array values stored in node has to be reflected on the .js file which has the ChartJS function.

感谢您的帮助/咨询

谢谢!

推荐答案

使用未转义的字符串插值和字符串化将您的pug变量转换为客户端浏览器变量:

You need to use unescaped string interpolation and a stringify to turn your pug variable into a client-side browser variable:

var arrayForChart = !{JSON.stringify(arrayInPug)};

如果express / pug中的变量为 ['a','b ','c'] 然后将输出:

If the variable in express/pug is ['a', 'b', 'c'] then this will output:

var arrayForChart = ['a', 'b', 'c'];

没有字符串化,您将得到类似这样的内容,它只会在浏览器中引发错误无效的JavaScript:

Without the stringify you'll get something like this which will just throw an error in the browser as it's not valid JavaScript:

var arrayForChart = [Object object];

这篇关于如何将数组从节点传递到.js文件(pugJs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆