antd不起作用(没有任何错误!)

时间:2018-08-21 15:16:04

标签: reactjs gulp antd

我正在使用最新版本的react和react-dom(16.4.2)和antd(3.8.2)。

我的文件如下:

index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>React Project</title>
  <link href="/assets/stylesheet/stylesheet.css" rel="stylesheet" type="text/css">
</head>

<body>
  <div id="main" >
  </div>
  <script src="/build/bundle.js"></script>
</body>

</html>

main.js

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {Select} from 'antd';
class App extends Component {
  render() {
    return (
      <div>
        salam
        <Select>
          <Option value="lucy">lucy</Option>
        </Select>
      </div>
    )
  }
}

ReactDOM.render(<App/>, document.getElementById('main'));

gulpfile.js

var gulp = require('gulp');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
// New Plugin
var notify = require('gulp-notify');
var util = require('gulp-util');
var watchify = require('watchify')
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');

gulp.task('browserify', function () {
  return browserify('./assets/scripts/main.js')
    .transform(babelify, {
      presets: ["es2015", "react"],
      "plugins": [
        ["import", {
          "libraryName": "antd",
          "style": "css"
        }]
      ]
    })
    .bundle()
    .on('error', function (e) {
      console.log(e.message);
      this.emit('end');
    })
    .pipe(source('bundle.js'))
    .pipe(buffer())
    .pipe(uglify())
    .pipe(gulp.dest('./build'));
});


gulp.task('watch', ['browserify'], function () {
  gulp.watch('./assets/scripts/**/*.js', ['browserify']);
});

问题:问题是,当我运行gulp watch并且gulpfile开始监视,然后在浏览器中时,除了以下警告之外,它没有任何错误:

  

bundle.js:8688您正在使用一整套antd,请使用https://www.npmjs.com/package/babel-plugin-import减小应用程序包的大小。

我只是在浏览器中看到“ salam”,而没有看到任何选择/选项组件。 我该如何解决此代码?

1 个答案:

答案 0 :(得分:0)

尝试添加此行。在官方示例中使用了它,如果不插入它,则会出错。

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {Select} from 'antd';

const Option = Select.Option;

class App extends Component {
   render() {
      return (
      <div>
         salam
            <Select>
                <Option value="lucy">lucy</Option>
            </Select>
      </div>
      )
   }
}