angularjs - electron + angular2 加载 vendor 库

标签 angularjs angular webpack electron

我使用 angularCLI 创建了一个新的 angular2 应用程序(只是为了让您知道我的目录结构)。

运行ng serve把我所有的文件放在dist文件夹并在浏览器中运行 hello world 应用程序没有问题。

我正在尝试在 Electron 中运行相同的应用程序,但它无法找到所有 vendor 文件(包括@angular),因为它在脚本 src 中使用文件协议(protocol):


<script src="vendor/es6-shim/es6-shim.js"></script>
<script src="vendor/reflect-metadata/Reflect.js"></script>
<script src="vendor/systemjs/dist/system.src.js"></script>
<script src="vendor/zone.js/dist/zone.js"></script>

产生这个
file:///vendor/es6-shim/es6-shim.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/reflect-metadata/Reflect.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/systemjs/dist/system.src.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/zone.js/dist/zone.js Failed to load resource: net::ERR_FILE_NOT_FOUND

如何在 file: 中添加正确路径? Electron 使用的协议(protocol)?

我的 gulpfile.js :
var gulp = require('gulp'),  
    del = require('del'),
    runSeq = require('run-sequence');

gulp.task('clean-electron', function(){  
    return del('dist/electron-package/**/*', {force: true});
});

gulp.task('copy:electron-manifest', function(){  
   return gulp.src('./package.json')
       .pipe(gulp.dest('./dist/electron-package'))
});

gulp.task('copy:electron-scripts', function(){  
    return gulp.src('./src/electron_main.js')
        .pipe(gulp.dest('./dist/electron-package'));
});

gulp.task('copy:vendor-for-electron', function() {
    return gulp.src('./dist/vendor/**/*')
        .pipe(gulp.dest('./dist/electron-package/vendor'))    
});

gulp.task('copy:spa-for-electron', function(){  
    return gulp.src(["./dist/*.*", "./dist/app/**/*"])
        .pipe(gulp.dest('dist/electron-package'));
});

gulp.task('electron', function(done){  
    return runSeq('clean-electron', ['copy:spa-for-electron', 'copy:vendor-for-electron', 'copy:electron-manifest', 'copy:electron-scripts' ], done);
});

我得到的最接近的是这样做:

我的 index.html:
<script src="vendor/es6-shim/es6-shim.js"></script>
<script src="vendor/reflect-metadata/Reflect.js"></script>
<script src="vendor/systemjs/dist/system.src.js"></script>
<script src="vendor/zone.js/dist/zone.js"></script>

<script>
 console.log("In my script tag:");
 var systemConfigPath = 'system-config.js';
 var mainPath = 'main.js';
 if (window.location.protocol == "file:"){
  require(__dirname + '/vendor/es6-shim/es6-shim.js');
  require(__dirname + '/vendor/reflect-metadata/Reflect.js');
  require(__dirname + '/vendor/systemjs/dist/system.src.js');
  require(__dirname + '/vendor/zone.js/dist/zone.js');

  systemConfigPath = __dirname + '/' + systemConfigPath; 
  mainPath = __dirname + '/' + mainPath ;
} 

System.import(systemConfigPath).then(function () {
    System.import(mainPath);
}).catch(console.error.bind(console));

但这仍然给我带来了问题,因为 vendor 文件引用了同一目录中的其他文件:

error

编辑:

我现在正在尝试使用 webpack 来构建我的 Electron 应用程序(没有成功)。

我还创建了 github repo如果您想查看代码。

最佳答案

来自 How should I configure the base href for Angular 2 when using Electron?答案是改变你

 <base href="/">


 <base href="./">

关于angularjs - electron + angular2 加载 vendor 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447020/

相关文章:

javascript - 没有状态的 Angularjs Controller 。如何添加依赖

javascript - Angularjs Bootstrap 日期选择器 : Select multiple dates

javascript - AngularJS 模式对话框未打开

python - 从 Angular 2 前端调用后端 python 脚本

javascript - Angular 7 找不到类型为 '[object Object]' 的不同支持对象 'object'。 NgFor 仅支持绑定(bind)到 Iterables,例如 Arrays

javascript - React newb 不知道我是否正确导入样式表

javascript - Angular - 检测点击背景 div 但不检测 div

Angular 组件中的 CSS 特性使用/深度/当类在组件外部时

javascript - 如何在 WordPress 站点内使用 PHP 来查找散列的 bundle.js 文件并将适当的文件名插入到脚本标记中?

javascript - 引用错误: bookshelf is not defined