簡體   English   中英

如何將來自 NodeJS API 的數據作為變量傳遞給 static JS 文件?

[英]How to pass data from NodeJS API as a variable to a static JS file?

I have made a NodeJS REST API, which returns JSON data of questions I have a static JS file which does DOM manipulations on client side to show different questions in an order, it takes questions from the questions array.

我能夠從 pug 渲染引擎上的 API 獲取數據並將其傳遞到主頁上,但無法找到將這些數據作為變量傳遞給在頁面上進行 DOM 操作的 JS 文件的方法。

我想到了一種從 static js 文件本身中的 API 獲取數據的方法,但這似乎不是一種有效的方法,因為對 API 的請求是在渲染引擎本身中完成的。

我的意見控制器:

視圖控制器

我的哈巴狗模板能夠訪問問題:

哈巴狗模板和渲染頁面

但我找不到將問題數組作為變量發送到執行 DOM 操作的 static JS 文件的方法。

我認為在 static js 文件中獲取 json 數據將是最好的選擇。 除此之外,您可以這樣做:

let questions = document.querySelectorAll('.overview-box__text').map((el) => JSON.parse(el.innerText));

暫無
暫無

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

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