我试图将一个API密钥--我在服务器端调用并传递给一个EJS文件--传递给脚本标记上的src属性。当我console.log字符串时,它完全按照我想要的方式打印,但是我不能以我需要的方式更新src属性,以便能够运行脚本。我尝试过直接传入字符串变量,使用模板字面量等。
<script>
let myKey = <%- JSON.stringify(key) %>
let string = `https://maps.googleapis.com/maps/api/js?key=${myKey}&callback=initMap`
</script>
<script src= https://maps.googleapis.com/maps/api/js?key=${myKey}&callback=initMap
async defer> </script
发布于 2020-01-15 09:46:51
您不能在标签定义中使用javascript,只能在script标签之间编写javascript,因此模板文字或变量在script标签的src值中不起作用
此外,您还可以在HTML部件中直接使用路由传递的值
在您的代码中像这样编写它
<script src="https://maps.googleapis.com/maps/api/js?key=<%= key%>&callback=initMap"
async defer> </script>
https://stackoverflow.com/questions/59740620
复制