javascript - 无法将对象从另一个 .js 文件传递到链接的 .js 文件...使用 Socket.io
问题描述
我不知道如何解决这个问题。如何将对象从服务器 .js 文件传递到公共 js 文件中。
带有 Socket.io 的 app.js
事件流程::
senderOfInvite > 点击屏幕名称(receiverOfInvite)锚标签 > receiverOfInvite > 接收有关边聊请求的模式 > receiverOfInvite 接受请求 > 带有 senderOfInvite.html 的新选项卡为接收者打开 > senderOfInvite 接收有关边聊接受的模式 > 单击“加入”> 带有 senderOfInvite 的新选项卡。 html 为 senderOfInvite 打开
这个新创建的 .html 页面是模板化的 <-- "src/template.js"。它调用“template-script.js”<----“public/js/template-script.js”。在“public/js/template-script.js”文件中有对“data.id”的调用,其中出现错误... :: Uncaught ReferenceError: data is not defined at template-script.js:1 ::
NODE.js:: "src/app.js"::
socket.on('register', (data) => {
let id = data.id;
let receiver = data.receiver;
let ns = io.of(`/${id}`);
fs.appendFile(`./public/users/${id}.html`, templatisize(id,receiver,nameofApp), function(err) {
if (err) throw err;
console.log(`${id}.html created`);
return;
});
NODE.js:: "src/template.js"::
function templatisize(id,receiver,title){
return `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../css/styles.css" />
<title> ${id} > ${receiver}</title>
<script src="https://cdn.socket.io/3.1.1/socket.io.min.js"></script>
</head>
<body>
<div class="prime-container">....
<script src="../js/template-script.js"></script>
</body>
</html>
`
}
module.exports = templatisize;
客户端:: "public/js/template-script.js"::
const socket = io(`/${data.id}`);
这是我得到错误的地方。 未捕获的 ReferenceError:数据未在 template-script.js:1 中定义
我的目标是从 src/app.js > public/js/template-script.js 获取数据
解决方案
推荐阅读
- json - HttpErrorResponse {headers:HttpHeaders,状态:415,statusText:“不支持的媒体类型”
- java - 有没有办法从 LWJGL 的内存中清除颜色?
- python - 比较火星漫游者模拟中的列表
- pytorch - 在 pytorch 优化器中手动更新动量项
- java - 是否可以在 switch 语句中设置条件?
- android - Kotlin - 从不返回布尔值的异步任务返回布尔值
- javascript - 为什么围绕原点的旋转会失去与原点的距离?
- rust - 在 match 语句中重用匹配的变量
- pandas - PyMongo group by:迭代组
- java - 如何在 Apache Tomcat 9.0.43 上访问以下管理员和经理角色 = manager-script、tomcat、admin-script