【问题标题】:Simple RequireJS, AngularJS, Electron App, Dependencies not Executing简单的 RequireJS、AngularJS、Electron App、依赖项未执行
【发布时间】:2015-12-14 08:06:47
【问题描述】:

我正在编写我的第一个 Electron/Angular/RequireJS 单页桌面应用程序。我从electron-boilerplate 开始,效果很好。但是,我需要将 AngularJs 添加到等式中——样板已经使用了 RequireJS。我尝试了许多解决方案,但要么出现错误,要么没有执行依赖项。所以我尝试从前面的堆栈溢出问题“Simple requireJS with angularJS - angular is not defined”中实现这个简单的例子。但是,我看到的是文件正在被解析,但它从不执行里面的代码。几乎就像一个或多个依赖项没有得到满足,所以它们没有执行。我的文件如下——我添加了一些日志记录,这样我就可以很容易地看到执行路径。

app.html -- 包含RequireJS的起点。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Electron Boilerplate</title>
</head>
<body ng-app="ReqApp" ng-controller="GreetCtrl">

    <h1>{{greeting}}!</h1>
    <script data-main="require_config" src="vendor/require.js"></script>

</body>
</html>

require_config.js -- 这包含 RequireJS 配置。

console.log("REQUIRE_CONFIG.JS-------------"+requirejs.version);    
requirejs.config({
    // alias libraries paths
  paths: {
    'domReady':      './vendor/requirejs-domready',
    'angular':       './node_modules/angular/angular',
    'GreetCtrl':     './js/GreetCtrl',
    'app':           './js/app'
  },
  // angular does not support AMD out of the box, put it in a shim
  shim: { 'angular': { exports: 'angular' }
  },
  // kick start application
  deps: ['./bootstrap']
});

bootstrap.js -- 来自 RequireJS deps 的文件,用于手动引导 AngularJS。

console.log("BOOTSTRAP.JS1-------------");
define([
    'require',
    'angular',
    'app'
], function (require, ng, app) {
    'use strict';

    console.log("BOOTSTRAP.JS2+++++++++++++");
    require(['domReady!'], function (document) {
        console.log("BOOTSTRAP.JS+++++++++++++");
        ng.bootstrap(document, ['ReqApp']);
    });
});

./js/app.js -- 这会为应用程序初始化 AngularJS 模块。

console.log("APP.JS-------------"+requirejs.version);
define([
  'angular',
  'GreetCtrl'
], function (ng) {
  'use strict';

  console.log("APP.JS+++++++++++++");
  return ng.module('ReqApp', [ 'ReqApp.GreetCtrl' ]);
});

./js/GreetCtrl.js

console.log("GREETCTRL.JS1-------------");
define(['angular'], function(angular) {
    console.log("GREETCTRL.JS1+++++++++++++");
    angular.module( 'ReqApp.GreetCtrl', [])
    .controller( 'GreetCtrl', function GreetCtrl ($scope) {
         $scope.greeting = "Hello World";
    });
});

运行此示例的控制台输出为:

[16982:0917/115914:ERROR:browser_main_loop.cc(173)] Running without the SUID sandbox! See https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment for more information on developing with the sandbox on.
[17010:0917/115915:INFO:renderer_main.cc(200)] Renderer process started
[17014:0917/115915:INFO:renderer_main.cc(200)] Renderer process started
[16982:0917/115915:ERROR:browser_gpu_channel_host_factory.cc(146)] Failed to create channel.
[16982:0917/115915:INFO:CONSOLE(43)] "REQUIRE_CONFIG.JS-------------2.1.20", source: file:///home/swdev/projects/build/require_config.js (43)
[16982:0917/115915:INFO:CONSOLE(8)] "BOOTSTRAP.JS1-------------", source: file:///home/swdev/projects/build/bootstrap.js (8)

页面输出为:

{{greeting}}!

从输出中可以看出,似乎只解析了 require_config.js 和 bootstrap.js 文件,但未执行定义中的代码。我的问题是:

  1. 为什么从不调用 bootstrap.js 函数?
  2. 为什么 app.js 和 GreetCtrl.js 文件甚至没有被解析?
  3. 由于 Electron 使用节点,我必须使用 requirejs,我是否需要从 bootstrap.js 中删除我对 require 的使用并用 requirejs 替换它?注意:我已经测试过更改此设置,但对我的问题没有任何帮助。
  4. 我确实注意到我遵循的stack overflow example 在HTML 正文定义中定义了ng-app="ReqApp"。我有限的理解是这应该被删除,因为我们手动引导 AngularJS。我的用例的正确方法是什么(Election(node)/RequireJS/AngularJS?注意:我已经通过从正文定义中删除定义进行了测试,但它对我的问题没有任何帮助。

我花了几天时间试图解决这个问题,所以任何帮助都会非常感激!


其他尝试: 多玩了一些来简化部分问题。我似乎无法让 RequireJS 执行或解析一些依赖项。我的简化是:

./app.js

<!doctype html>
<html>
<head>
    <title>Electron Boilerplate</title>
</head>
<body >
    <script data-main="require_config" src="vendor/require.js"></script>
    <h1>Hello World!</h1>
</body>
</html>

./require_config.js

console.log("REQUIRE_CONFIG.JS-------------"+requirejs.version);
requirejs.config({
    paths: {
    'app': './js/app'
  },
  // kick start application
  deps: ['./bootstrap']
});

./bootstrap.js

console.log("BOOTSTRAP.JS1-------------");
define( ['app'], function (app) {
    'use strict';
    console.log("BOOTSTRAP.JS2+++++++++++++");
});

./js/app.js

console.log("APP.JS-------------");
define([], function () {
  'use strict';
  console.log("APP.JS+++++++++++++");
});

生成的输出仍然与我上面的原始输出相同。 boostrap.js 和 require_config.js 被处理。 bootstrap.js 的定义/函数永远不会被调用,app.js 永远不会被解析。

【问题讨论】:

    标签: javascript angularjs node.js requirejs electron


    【解决方案1】:

    contactingElectron boilerplate 的开发者之后,我认为他让我直截了当。主要问题是我对样板的所有移动部分缺乏了解。在尝试扩展样板文件时,我当时没有意识到/理解他的应用程序是使用 ES6 编写的,该 ES6 被转换为兼容的 RequireJS 格式。因此,我遇到的问题根本与 RequireJS 无关。我编写的模块是 RequireJS 兼容的,然后被转译,这意味着 RequireJS 无法处理它们。

    正确的做法是编写 ES6 模块,问题就消失了。 hack 解决方法是使用符合 RequieJS 的语法添加您的模块,然后修改样板 build.js copyFromAppDir 以排除这些文件被转译。但同样,为了遵循样板的原始意图和设计,代码应该使用 ES6 编写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多