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

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

问题描述

晚上,
我正在研究使用电子来打包现有的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

Create Project
Use angular-cli to start a new project ng new electron-ng2-cli --style=scss

安装电子和电子生成器

编辑package.json
添加以下内容...
"main": "main.js"

Edit package.json
Make the following additions...
"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
我刚刚从

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

修改基础
index.html中,将<base="/">更改为<base="./">

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

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

测试运行
运行npm run 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/<username>/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.

我通过解压缩 app.asar文件找到了问题electron-builder产生的.而不是包含我的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的路径.
下一步,我也在src中创建了一个新的应用程序级别package.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文件夹.
最后,我进入了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.jspackage.json捆绑到build文件夹中.
完成此操作后,我可以运行npm run electron来快速启动测试. app或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天全站免登陆