2017-07-31 66 views
0

我的目錄結構如下:一飲而盡,注入不能注入js文件中的index.html

|- app 
    |- bower_components 
    |- css 
    |- img 
    |- js 
    |- scss 
    |- template 
|- node_modules 
|- gulpfile.js 
|- package.json 
|- .bowerrc 
|- bower.json 

現在我gulpfile.js如下圖所示:

gulp.task('index', function() { 
    var target = gulp.src('./app/index.html'); 
    var sources = gulp.src(['app/js/**/*.js'], { read: false }); 

    return target.pipe(inject(sources)) 
     .pipe(gulp.dest('app/js')); 
}); 


gulp.task('default', function(callback) { 
    runSequence(['sass', 'browserSync', 'index'], 'watch', 
     callback 
    ); 
}); 

我的指數.html文件如下所示:

<!DOCTYPE html> 
<html> 

<head> 
    <title>kisanApp</title> 
    <!-- inject:css --> 
    <!-- endinject --> 
</head> 

<body ng-app="kisanApp"> 
    <ng-view></ng-view> 
    <!-- inject:js --> 
    <!-- endinject --> 
    <script src="bower_components/angular/angular.js"></script> 
    <script src="bower_components/angular-route/angular-route.js"></script> 
</body> 

</html> 

但不知怎的,我的js文件沒有通過gulp添加到index.html中。那麼路徑有什麼問題嗎?

回答

1

嘗試將var inject = require('gulp-inject');var gulp = require('gulp');添加到您的gulp文件的頂部。

然後再看看你的源代碼和gulp.dest:

var sources = gulp.src(['./app/js/**/*.js'], { read: false });

您需要的應​​用程序之前進行./

編輯:

使用relative true從文件夾中處理文件注入目錄樹中的上漲

var sources = gulp.src(['./app/js/**/*.js'], { read: false }, {relative: true});

+0

它增加了** **而不是** ** –

+1

嘗試'var sources = gulp.src(['./ app/js/**/*。js'] ,{read:false},{relative:true});'ref:https://github.com/klei/gulp-inject/wiki/Clarifying-injected-paths – dCrux

+0

我試過了你的代碼。有效! –