首页 > 解决方案 > 无法将对象从另一个 .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 获取数据

标签: javascriptobjectsocket.io

解决方案


推荐阅读