电子 - 不允许加载本地资源 [英] Electron - Not allowed to load local resource

查看:50
本文介绍了电子 - 不允许加载本地资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上,
我正在研究使用 electron 来打包现有的 angular2 构建.我以为我进行了试运行,但实际包装似乎失败了(请参阅下面的最后一步),我想了解原因.这就是我正在做的事情......

Evening,
I'm looking into using electron to package an existing angular2 build. I thought I had a dry run working but the actual packaging seems to be failing (see final step below) and I want to understand why. Here's what I'm doing...

创建项目
使用 angular-cli 开始一个新项目 ng new electron-ng2-cli --style=scss

安装 electron 和 electron-builder

Install electron and electron-builder

编辑 package.json
进行以下添加...
"main": "main.js"

"build":
{
  "appId": "com.electrontest.testapp",
  "mac": {
    "category": "your.app.category.type"
  }
}

并将以下内容添加到 scripts...

and add the following to the scripts...

"pack": "build --dir",
"dist": "build",
"electron": "electron main.js",
"postinstall": "install-app-deps"

创建 main.js
我刚刚从 电子快速入门.我所做的唯一更改是 index.html 的位置,我将其设置为 /dist/index.html

Create main.js
I just copied the code from the electron quick start. The only change I make is to the location of index.html which I set to /dist/index.html

修改 base
index.html 中将 更改为

打包代码
运行ng build.这会将所有打包的代码放在 /dist

Pack code
Run ng build. This puts all the packaged code in /dist

测试运行
运行 npm run electron.这工作正常.一个 Electron 应用程序启动,我看到其中运行有棱角的东西.

Test Run
Run npm run electron. This works fine. An Electron app fires up and I see the angular stuff running within it.

为分发创建应用程序
运行 npm run pack 以创建打包的应用程序.包装似乎没问题 - 我收到关于缺少图标的警告和我的代码未签名的警告,但我猜它们不应该是致命的?
问题是,当我现在通过双击 Finder 运行应用程序时,我在控制台中收到错误消息:Not allowed to load local resource: file:///Users/<用户名>/Documents/development/electron-ng2-cli/dist/mac/electron-ng2-cli.app/Contents/Resources/app.asar/dist/index.html<小时>那么,当我使用 npm run electron 时,任何人都可以解释失败的打包应用程序和运行正常的应用程序之间有什么不同吗?

Create App For Distribution
Run npm run pack to create a packaged app. The packaging seems to go ok - I get a warning about a missing icon and a warning that my code is unsigned but I'm guessing they shouldn't be fatal?
The problem is that when I now run the app by double clicking in Finder I get an error in the console saying: Not allowed to load local resource: file:///Users/<username>/Documents/development/electron-ng2-cli/dist/mac/electron-ng2-cli.app/Contents/Resources/app.asar/dist/index.html


So, can anyone explain what is different between the packaged app that fails and the one that runs ok when I use npm run electron?

我可以做些什么来解决这个问题并让应用程序正常运行?

What can I do to fix this issue and get the app running correctly?

感谢您坚持到最后.这比我想要的要长,但我希望我能解释清楚.如果您可以提供帮助或提供任何建议,那就太好了 - 在您的大方向上会考虑很多好的氛围:)

Thank you for making it to the end. This got longer than I wanted but I hope I explained myself ok. If you can help or give any pointers that would be great - many good vibes will be thought in your general direction :)

祝大家好运

推荐答案

经过大量的试验和错误,但我得到了这个工作.这里有很多我不完全理解的东西,而且很多可能是毫无意义或不好的做法,而且它可能会在下一步全部失败,但如果像我一样,你只是想克服第一个驼峰也许我找到的东西会对你有所帮助.

It took a lot of trial and error but I got this working. There is a lot here that I don't totally understand, and much that might be pointless or bad practice and it might all fall down at the very next step but if, like me, you are just trying to get over the first hump then maybe something I found will help you.

我通过解包 应用程序发现了问题electron-builder 生成的 .asar 文件.它没有包含我的 dist 文件夹中的捆绑代码,而是包含所有项目代码(*.ts *.scss 等).问题只是打包函数打包了错误的东西.
将正确的源代码放入最终应用程序的步骤很简单,但天哪,他们没有半途而废!从我在最初的问题中停止的地方开始,我做了以下......

I found the problem by unpacking the app.asar file that electron-builder produces. Instead of containing the bundled code from my dist folder it contained all the project code (*.ts *.scss etc). The problem was just that the packing functions were packing up the wrong stuff.
The steps to get the right source into the final app are simple when you lay them out but my god they didn't half put up a fight! Starting from where I left off in my initial question I did the following...

记住我的项目结构是angular-cli设置的默认结构

电子特定文件
我将 main.js 移至 src 并将其名称更改为 electron-main.js 只是为了避免与已经存在的 main.ts 混淆.我还将它引用的路径更改回 /index.html.
接下来我也在 srcpackage.json
> 并给它以下内容:

Electron specific files
I moved the main.js down into src and changed its name to electron-main.js just to stop any confusion with the main.ts that is already in there. I also changed the path it references back to /index.html.
Next I created a new application level package.json also in src and gave it the following content:

 {
  "name": "application-name",
  "description": "CLI app",
  "author": "Me me me",
  "version": "0.0.1",
  "main": "electron-main.js"
}

angular-cli.json
我将 outDir 更改为 build 纯粹是因为电子似乎输出到 dist 默认情况下,我想在这个阶段进行一些分离.然后我将 package.jsonelectron-main.js 添加到 assets 数组中.

angular-cli.json
I changed the outDir to build purely because electron seems to output to dist by default and I wanted some separation at this stage. Then I addded package.json and electron-main.js to the assets array.

Main package.json
我删除了 "main":"main.js",因为这里显然不再需要它.在 scripts 中,我将测试命令更改为 electron build 以将其指向捆绑代码所在的构建文件夹.
最后,我去了 build 字段并添加以下内容:

Main package.json
I removed the "main":"main.js" as it is apparently no longer needed here. In scripts I changed the test command to electron build to point it at the build folder where the bundled code will be.
Finally, I went to the build field and added the following content:

"directories": {
  "buildResources": "build",
  "app": "build"
}

现在看起来很明显.这只是告诉编译器在哪里寻找构成最终应用程序的资产.它默认为工作目录,这是我的问题.

Seems pretty obvious now. This just tells the compiler where to look for the assets that will make up the final app. It was defaulting to the working directory and that was my problem.

使用此设置,我现在可以运行 ng build 将我的项目与 electron-main.js 一起捆绑到 build 文件夹中,并且package.json.
完成后我可以运行 npm run electron 快速启动测试应用或 npm run pack 构建应用可以从 Finder 启动.

Using this setup I can now run ng build to bundle my project into the build folder along with the electron-main.js and package.json.
This done I can run npm run electron to quickly launch a test app or npm run pack to build an app that can be launched from Finder.

轰隆隆.任务完成.我会在十分钟后回到这里,回答我期待的另一个烦人的问题.这些天我一定很喜欢这个开发者:)

Boom. Job done. I'll be back here in ten minutes with another annoying question I expect. Gotta love the dev these days :)

这篇关于电子 - 不允许加载本地资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆