简体   繁体   English

如何从玉到本地javascript使用对象JSON?

[英]How to use a object JSON from jade to local javascript?

I try to use a JSON object (all the object not just a piece) from Node through Jade inside my local myScript.js script. 我尝试在本地myScript.js脚本中通过Jade使用来自Node的JSON对象(所有对象而不仅仅是一个对象)。

And my Jade file: 还有我的Jade文件:

span(class="glyphicon glyphicon-pencil" onclick="confirm(\"#{myJSON.taskid}\", \"update\", !{myJSON})")

But I have this: 但是我有这个:

Uncaught ReferenceError: myJSON is not defined. 未捕获ReferenceError:未定义myJSON。

Then I quit !{myJSON} as parameter in the call of function confirm() in onclick() , and I try with a script block: 然后,我在onclick()的函数!{myJSON} confirm()的调用中退出!{myJSON}作为参数,然后尝试使用脚本块:

script(type= "text/javascript" src="myScript.js").
    var local_json =!{JSON.stringify(myJSON)};

In myScript.js I issued console.log(local_json) , but I end up with the following error in the console: Uncaught ReferenceError: local_json is not defined . myScript.js我发出了console.log(local_json) ,但最终在控制台中出现以下错误: Uncaught ReferenceError: local_json is not defined

How I can do it? 我该怎么办?

You have to first create variable , then import your script file. 您必须首先创建变量,然后导入脚本文件。 so basically var local_json = !{JSON.stringify(myJSON)} then import other script - script ( type = "text/javascript" src="myScript.js") 所以基本上是var local_json =!{JSON.stringify(myJSON)}然后导入其他脚本-脚本(类型=“ text / javascript” src =“ myScript.js”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM