无法通过转换为babel来访问它

时间:2017-06-16 09:24:34

标签: javascript node.js ecmascript-6 babel

我正在使用es6功能并在nodeJs上使用babel进行编译,但似乎我无法从其他方法访问实例

export default class TimeRouter {
  router : Router;
  path : string;

constructor(path:string='/api/v1/timerouter'){
  this.router = Router();
  this.path = path;

  this.init();

  this.timeRanges = {
   'LAST_SEMESTER' : 6, //nb of months,
   'LAST_12_MONTHS' : 12,
   'LAST_TRIMESTER' : 4,
   'LAST_YEAR' : 12 
  }

 }


 getTimeRanges(req: $Request, res:$Response): void {


  console.log('Test ranges',this); //returns undefined

  res.status(200).json(this.timeRanges);
 }


 init():void {
  this.router.get('/',this.getTimeRanges)
 }
}

使用gulp-sourcemaps和gulp-babel,我得到了这个gulp配置:


    gulp.task('scripts', () => {
      return gulp.src('src/**/*.js')
      .pipe(sourcemaps.init())
      .pipe(babel())
      .pipe(sourcemaps.write('.'))
      .pipe(gulp.dest('build'));
    });

它呈现如下:


    'use strict';

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i  0 && arguments[0] !== undefined ? arguments[0] : '/api/v1/timerouter';

        _classCallCheck(this, TimeRouter);

        this.router = (0, _express.Router)();
        this.path = path;

        this.init();

        this.timeRanges = {
          'LAST_SEMESTER': 6, //nb of months,
          'LAST_12_MONTHS': 12,
          'LAST_TRIMESTER': 4,
          'LAST_YEAR': 12
        };
      }

      _createClass(TimeRouter, [{
        key: 'getTimeRanges',
        value: function getTimeRanges(req, res) {
          console.log(this.timeRanges);
          var ranges = this.timeRanges;

          res.status(200).json(ranges);
        }
      }, {
        key: 'init',
        value: function init() {
          this.router.get('/', this.getTimeRanges);
        }
      }]);

      return TimeRouter;
    }();

    exports.default = TimeRouter;
    //# sourceMappingURL=TimeRouter.js.map

为什么我无法访问当前实例的任何想法?

0 个答案:

没有答案