前端技能汇总 Frontend Knowledge Structure
深入浅出Node.js 书籍pdf 《深入浅出Node.js》的相关代码
方法1;
在html文件中加入两个脚本程序,注意,加入的位置在和两个标签之间,(也有的在两个标签之间加入的),代码如下:
</head>
<script src="hello.js"></script>
<script src="renderer.js"></script>
<body>
之后在hello.js中直接调用函数就行。
test('click', 'asynchronous message', 'ping');
方法2
renderer.js中使用exports导出函数:
//在这里面写好函数的封装,然后在hello.js中调用
var test = function(struct, buttonId, msg){
const asyncMsgBtn = document.getElementById(buttonId);
asyncMsgBtn.addEventListener(struct, function(){
switch(struct){
case 'click':
ipc.send('asynchronous-message', msg);
console.log("调用成功");
break;
default:
console.log('Error!!!')
}
})
}
//这种方式是成功的
exports.test = test;
//这种方式也是可以得
//module.exports.test = test;
而hello.js中对于代码的使用如下:
//利用require加载模块
const renderer = require('./renderer')
renderer.test('click', 'asynchronous message', 'ping');
renderer.test('click', 'changeView', 'change');