【问题标题】:How to get started with angular2 in nw.js如何在 nw.js 中开始使用 angular2
【发布时间】:2016-04-22 09:36:07
【问题描述】:

如何在 nw.js 下为 angular 2 获得一个工作的“hello world”?

我使用本指南成功获得了一个工作 angular 2 应用程序:

https://angular.io/docs/js/latest/quickstart.html

和一个使用这个的工作 nw.js 应用程序:

https://egghead.io/lessons/javascript-your-first-nw-js-desktop-application-in-less-than-5-minutes

将两者结合我得到:

package.json:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  },
  "description": "",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": ""
}

index.html:

<html>

  <head>
    <title>Angular 2 QuickStart</title>

    <!-- 1. Load libraries -->
    <script src="env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="env/lib/node_modules/angular2-quickstart/node_modules/rxjs/bundles/Rx.umd.js"></script>
    <script src="env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-all.umd.js"></script>

    <!-- 2. Load our 'modules' -->
    <script src='app/app.component.js'></script>
    <script src='app/boot.js'></script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>

app/boot.js:

(function(app) {
  document.addEventListener('DOMContentLoaded', function() {
    ng.platform.browser.bootstrap(app.AppComponent);
  });
})(window.app || (window.app = {}));

app/app.component.js:

(function(app) {
  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',
      template: '<h1>My First Angular 2 App</h1>'
    })
    .Class({
      constructor: function() {}
    });
})(window.app || (window.app = {}));

为了安装我运行的节点和依赖项(我最终得到了节点 5.4.1;nodeenv 是 https://pypi.python.org/pypi/nodeenv/0.13.6):

nodeenv --prebuilt env
source env/bin/activate
npm install -g nw
npm install -g

然后在nw下运行app:

nw .

如果我用简单的纯 HTML 'hello world' 替换 index.html,我会在 nw 中看到该显示。但是,使用 angular 2 index.html,我在浏览器中看到来自我的应用程序的“正在加载...”消息,并且在我启动 nw 的终端上:

[22627:0117/010210:ERROR:browser_main_loop.cc(170)] Running without the SUID sandbox! See https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment for more information on developing with the sandbox on.
ATTENTION: default value of option force_s3tc_enable overridden by environment.
[22627:0117/010210:ERROR:nw_shell.cc(336)] TypeError: Cannot read property 'prototype' of undefined
    at Object.apply (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:941:35)
    at Object.apply (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:835:22)
    at Object.apply (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:301:27)
    at Object.<anonymous> (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:38:14)
    at Object.1.../core (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:40:4)
    at s (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:19:254)
    at e (file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:19:425)
    at file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js:19:443
[22627:0117/010210:INFO:CONSOLE(941)] "Uncaught TypeError: Cannot read property 'prototype' of undefined", source: file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-polyfills.js (941)
[22627:0117/010211:ERROR:nw_shell.cc(336)] reflect-metadata shim is required when using class decorators
[22627:0117/010211:INFO:CONSOLE(2398)] "Uncaught reflect-metadata shim is required when using class decorators", source: file:///home/me/dev/angular2-quickstart/env/lib/node_modules/angular2-quickstart/node_modules/angular2/bundles/angular2-all.umd.js (2398)
[22627:0117/010211:ERROR:nw_shell.cc(336)] ReferenceError: ng is not defined
    at window.app.window.app (file:///home/me/dev/angular2-quickstart/app/app.component.js:3:5)
    at file:///home/me/dev/angular2-quickstart/app/app.component.js:10:3
[22627:0117/010211:INFO:CONSOLE(3)] "Uncaught ReferenceError: ng is not defined", source: file:///home/me/dev/angular2-quickstart/app/app.component.js (3)
[22627:0117/010211:ERROR:nw_shell.cc(336)] ReferenceError: ng is not defined
    at HTMLDocument.<anonymous> (file:///home/me/dev/angular2-quickstart/app/boot.js:3:5)
[22627:0117/010211:INFO:CONSOLE(3)] "Uncaught ReferenceError: ng is not defined", source: file:///home/me/dev/angular2-quickstart/app/boot.js (3)
[22656:0117/010213:WARNING:channel.cc(553)] Failed to send message to remove remote endpoint (local ID 2147483649, remote ID 3)
[22656:0100/000000:WARNING:channel.cc(553)] Failed to send message to remove remote endpoint (local ID 1, remote ID 1)

我在 Linux 上。

【问题讨论】:

  • 在开发者控制台中查看 net 选项卡时是否加载了角度?
  • @LarryLane 我怎么知道?在控制台输入 ng?我得到一个 Uncaught ReferenceError: ng is not defined

标签: javascript node.js angular nw.js


【解决方案1】:

您的 Angular JavaScript 文件似乎没有被加载。这就是我相信 ng 未定义的原因。

要为开发目的启用工具栏,请将以下属性设置放在您的 package.json 文件中。

 "toolbar": true,

查看以下屏幕截图,以便开始进行故障排除。

点击网络标签后,按 F5 刷新页面,以便查看哪些文件已加载,哪些文件尚未加载。

【讨论】:

  • 虽然我没有看到任何网络流量记录有角度 JS 文件,但我认为这不可能是因为它们没有被加载。那是因为在控制台上,它会打印遇到问题的某些 Angular JS 文件的行(请参阅我的原始问题)。它如何在未加载的文件的特定行遇到问题?我项目中的两个.js文件也是如此。
  • 您没有看到任何网络流量记录是什么意思?您应该会在网络选项卡下看到加载的 javascript 文件。
  • 我的意思是我没有看到在网络选项卡下加载了任何文件。
  • 很抱歉,在您单击网络选项卡后,我忘了告诉您按 f5。然后你会看到哪些文件已经加载,哪些还没有加载。
  • 哦,好的,谢谢。我看到加载了以下所有内容:index.html、angular2-polyfills.js、Rx.umd.js、angular2-all.umd.js、app.component.js 和 boot.js
猜你喜欢
  • 1970-01-01
  • 2017-01-07
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
相关资源
最近更新 更多