【electron】学习(二)

遇到的坑

1.在全局安装electron的时候,遇到安装卡住的情况,一般来说是要改变一下electron的镜像地址
首先,打开cmd,输入npm config edit,这是会弹开一个记事本(txt).npmrc,然后在里面加一句electron_mirror="https://npm.taobao.org/mirrors/electron/"即可。
2.当引入const fs= require('fs');的时候会报错Uncaught ReferenceError: require is not defined,可能是版本的问题,所有要在配置项添加contextIsolation:false才可正常引入。

Api例子:

一、文件引入

在入口文件要配置允许node使用的项:nodeIntegration: true,
upload.js

//这次会引用到第三方库fs,通过fs的readFile方法去读取文件内容并展示出来
const fs = require('fs');
let upload = document.querySelector('.upload');
let detail = document.querySelector('.detail');
upload.addEventListener('drop', (e) => {
  e.preventDefault();//阻止默认行为
  e.stopPropagation();//阻止冒泡
  for (const f of e.dataTransfer.files) {
    fs.readFile(f.path, (err, data) => {
      let newDetail = document.createElement('div');
      newDetail.innerHTML = data;
      detail.appendChild(newDetail)

    })
  }
})
upload.addEventListener('dragover', (e) => {
  e.preventDefault();
  e.stopPropagation();
})

upload.html

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="upload">
  </div>
  <div class="detail"></div>
  <script src="./upload.js"></script>
</body>

</html>
<style>
  .upload {
    width: 200px;
    height: 200px;
    background-color: pink;
  }
</style>

二、webview内嵌

在入口文件要配置允许webview内嵌使用的项:webviewTag: true
index.html

<webview src="https://www.baidu.com" style="width:400px;height:300px"></webview>

render.js

let webview = document.querySelector('webview');
webview.addEventListener('did-start-loading', () => {
  console.log('start');
}) //开始加载
webview.addEventListener('did-stop-loading', () => {
  console.log('stop');
  webview.insertCSS('#su{background:red !important;}');
}) //结束加载

具体所需要的webview方法和属性在官网可查询https://www.electronjs.org/zh/docs/latest/api/webview-tag#webviewinsertcsscss

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值