2015-10-18 36 views
1

我正在開發一個使用ecma6標準的反應應用程序。我如何捆綁反應的應用程序,需要jquery ajax調用

var gulp = require('gulp'); 
 
var browserify = require('browserify'); 
 
var babelify = require('babelify'); 
 
var source = require('vinyl-source-stream'); 
 
var glob = require('glob'); 
 

 
gulp.task('build', function() { 
 

 
    browserify({ 
 
    entries: 'index.jsx', 
 
    extensions: ['.jsx'], 
 
    debug: true 
 
    }) 
 
    .transform(babelify) 
 
    .bundle() 
 
    .pipe(source('bundle.js')) 
 
    .pipe(gulp.dest('dist')); 
 
}); 
 
    
 
gulp.task('default', ['build']);

現在我有一個使用jQuery的AJAX調用,像這樣的反應成分之一:現在我創建一個捆綁在瀏覽器中使用下面的gulpfile.js使用

componentDidMount() { 
 
    $.ajax({ 
 
     url: this.props.url, 
 
     dataType: 'json', 
 
     cache: false, 
 
     success: function(data) { 
 
     this.setState({data: data}); 
 
     }.bind(this), 
 
     error: function(xhr, status, err) { 
 
     console.error(this.props.url, status, err.toString()); 
 
     }.bind(this) 
 
    }); 
 
    }

如何修改我的一飲而盡文件,以便jQuery代碼可以包含在反應代碼中?

+0

如果你只想要jQuery的Ajax你可能不需要jQuery:http://youmightnotneedjquery.com/。例如,您可以使用superagent或axios。您可以從sitepoint閱讀本文中的更多內容:http://www.sitepoint.com/comparison-javascript-http-libraries/ –

回答

0

我可以按照以下步驟來使用jQuery在我的代碼:

1)使用命令

NPM安裝jQuery的包中安裝jQuery

2)導入包中你想使用jQuery組件的語句

從'jquery'導入$;

相關問題