electron另一种运行方式

编写helloword

全局安装软件

 npm install -g electron

快速编写html

 html:5

 

 

完整代码和流程:

1.index.html

 <!DOCTYPE html>
 <html lang="en">
 <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>
    <H1>hello word</H1>
 </body>
 </html>

2.main.js

 var electron=require('electron')
 var app=electron.app//引用app
 var BrowserWindow=electron.BrowserWindow//窗口引用
 var mainWindow=null//声明打开的主窗口
 app.on('ready',()=>{
    mainWindow=new BrowserWindow({width:300,height:300})
    mainWindow.loadFile('index.html')//加载html
    mainWindow.on('closed',()=>{
        mainWindow=null
    })
 })
 

3.生成package.json

 npm init --yes 

后生成的json会根据本地的main.js改名

4.运行代码

 electron .

5.出现异常进行积极解决

 PS C:\Users\player\Desktop\nodeapp> electron .
 electron : 无法加载文件 C:\Users\player\AppData\Roaming\npm\electron.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_
 Execution_Policies。
 所在位置 行:1 字符: 1
 + electron .
 + ~~~~~~~~
    + CategoryInfo         : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

解决方案:解决 cnpm : 无法加载文件 C:\Users\hp\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。

 以管理员身份运行power shell
 输入set-ExecutionPolicy RemoteSigned
 选择修改执行策略就能运行了

 

posted @ 2021-02-07 00:37  三号小玩家  阅读(181)  评论(0编辑  收藏  举报
Title
三号小玩家的 Mail: 17612457115@163.com, 联系QQ: 1359720840 微信: QQ1359720840