简体   繁体   中英

antd doesn't work (without any error!)

I'm using the latest version of react and react-dom (which is 16.4.2) and antd (3.8.2).

My files are as follows:

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']);
});

Problem: The problem is that when I run gulp watch and the gulpfile start watching and then in the browser it doesn't have any errors except the following warning:

bundle.js:8688 You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size.

I just see the 'salam' in the browser and I don't see any select/option component. How should I fix this code?

Try to add this line. It is used in the official examples and I have an error if I don't insert it.

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>
      )
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM