【问题标题】:how to use app.locals. parameter inside javascript?如何使用 app.locals。 javascript中的参数?
【发布时间】:2018-10-15 18:19:45
【问题描述】:

我正在开发一个 node.js 应用程序。我在 app.js 中加载了一个 JSON 文件作为 app.locals.parameter。 然后我试图在我的 index.hbs 中使用它。

假设:

app.locals.componentData = require('./myfile.json');

当我在下面的 HTML 部分中使用参数时,它运行正常。

 {{#each componentData.categories}}
        <li id="{{this.categoryName}}" name="{{this.categoryName}}" class="btn btn-primary btn-sm" style="position: relative; z-index: 10; margin-bottom:3px" draggable="true" role="option" aria-grabbed="false"><div><img src="images/plugin.png" alt="{{this.categoryDescription}}"/>{{this.categoryName}}</div></li>
 {{/each}}

但是当我尝试在我的 javascript 函数中使用该参数时,它无法识别它。

function InitComponents() {
         for( var i = 0; i < componentData.categories.length; i++ ){
             alert("in loop!"+i+"-"+componentData.categories[i].categoryName);
              if(componentData.categories[i].categoryName.match(itemId)){
              alert("here:"+componentData.categories[i].parameters.length);
          createParameterList(newID,componentData.categories[i].parameters);
                        }

            }

    }

我的 JSON 格式如下:

{
    "categoryName": "xyz",
    "categories": [
      {
        "categoryName": "ABC", 
        "parameters": [
          {
            "ParameterID": "ID",
            "ParameterName": "ID",
            "ParameterDefultValue": "",
            "ParameterValue": "",
            "ParameterDescription": "ID Description"
          }]
}]
}

我可以知道为什么系统无法理解吗 componentData.categories.length 在我的脚本部分?

【问题讨论】:

    标签: javascript html node.js json


    【解决方案1】:

    这里的问题是InitComponents 函数对app.locals 一无所知。 app.locals 变量在模板中被识别。为了使您的代码正常工作,您需要将app.locals.componentData 作为参数传递给InitComponents 函数:

    function InitComponents(componentData) {
         for( var i = 0; i < componentData.categories.length; i++ ){
             alert("in loop!"+i+"-"+componentData.categories[i].categoryName);
              if(componentData.categories[i].categoryName.match(itemId)){
                alert("here:"+componentData.categories[i].parameters.length);
                createParameterList(newID,componentData.categories[i].parameters);
              }
         }
    }
    

    现在你可以像这样调用这个函数了

    InitComponents(app.locals.componentData);
    

    InitComponents 现在将真正知道componentData 是什么。 如果您在中间件中使用此功能,您可以通过req.app 访问app

    myMiddlewareFunction((req, res, next) => {
        const app = req.app;
        initComponents(app.locals.componentData);
        // some other logic
    })
    

    【讨论】:

    • @volodymry:感谢您的回答。如您所说,我添加了更改,但现在在我的名为 Initial 组件的按钮中,我收到了此错误:“未捕获的 ReferenceError:未定义应用程序。”另外,你能解释一下你答案的最后一部分吗,我没明白。我应该在哪里添加它?
    • @Amir 你能分享一下你调用InitComponents函数的函数的代码吗?
    • @Volodymry:我这样称呼它: [以前] 和现在 =>
    • 我想,我误解了你的问题。我以为您正在尝试在 NodeJS 端的 JS 中使用 componentData。但据我所知,您正在尝试在前端脚本中使用componentData。如果是这样,您需要从服务器获取此 JSON 文件并将其分配给脚本内的变量。或者,您可以使用 javascript 在模板中插入 &lt;script&gt; 标记,将 componentData 分配给全局变量。
    • 您能否根据您的新理解修改您的答案。因为我是 NodeJS 的新手。正如我上面提到的,我在 app.js 中的 app.locals.componentData 中加载了 JSON;我可以在 HTML 部分成功使用 componentData,但在脚本部分我不知道如何使用它。 :(
    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多