Aurelia + Typescript + @bindable

Jac*_*que 2 typescript aurelia

我开始学习Aurelia并且我在他们的网站上关注入门指南但是我使用的是Typescript而不是Javascript.一切都很好,但我有一个问题,使@bindable装饰工作在nav-bar组件中.

我的设置如下:

  • Visual Studio 2015(ASP.NET vNext项目)
  • 打字稿1.5.3
  • JSPM 0.16.0 beta 7
  • SystemJS 0.18.9
  • Aurelia(框架:0.15.0;引导程序:0.16.0)

这是我的nav-bar.ts文件的内容:

import {bindable} from "aurelia-framework";
import {Router} from "aurelia-router";

export class NavBar {
      @bindable router: Router = null;
}
Run Code Online (Sandbox Code Playgroud)

我有一个tsconfig.json文件如下:

{
   "compilerOptions": {
      "noImplicitAny": false,
      "noEmitOnError": false,
      "removeComments": false,
      "target": "es5",
      "module": "system",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true
   }
}
Run Code Online (Sandbox Code Playgroud)

如果我让VS转换Typescript文件,生成的nav-bar.js文件如下所示:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
    }
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};System.register(['aurelia-framework', 'aurelia-router'], function(exports_1) {
    var aurelia_framework_1, aurelia_router_1;
    var NavBar;
    return {
        setters:[
            function (_aurelia_framework_1) {
                aurelia_framework_1 = _aurelia_framework_1;
            },
            function (_aurelia_router_1) {
                aurelia_router_1 = _aurelia_router_1;
            }],
        execute: function() {
            NavBar = (function () {
                function NavBar() {
                    this.router = null;
                }
                __decorate([
                    aurelia_framework_1.bindable, 
                    __metadata('design:type', aurelia_router_1.Router)
                ], NavBar.prototype, "router");
                return NavBar;
            })();
            exports_1("NavBar", NavBar);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用gulp-typescript,结果是一样的.使用这两个解决方案,绑定不起作用,导航栏为空.

如果我使用gulp-babel,生成的nav-bar.js文件如下所示:

System.register(["aurelia-framework", "aurelia-router"], function (_export) {
    "use strict";

    var bindable, Router, NavBar;

    var _createDecoratedClass = (function () { function defineProperties(target, descriptors, initializers) { for (var i = 0; i < descriptors.length; i++) { var descriptor = descriptors[i]; var decorators = descriptor.decorators; var key = descriptor.key; delete descriptor.key; delete descriptor.decorators; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor || descriptor.initializer) descriptor.writable = true; if (decorators) { for (var f = 0; f < decorators.length; f++) { var decorator = decorators[f]; if (typeof decorator === "function") { descriptor = decorator(target, key, descriptor) || descriptor; } else { throw new TypeError("The decorator for method " + descriptor.key + " is of the invalid type " + typeof decorator); } } if (descriptor.initializer !== undefined) { initializers[key] = descriptor; continue; } } Object.defineProperty(target, key, descriptor); } } return function (Constructor, protoProps, staticProps, protoInitializers, staticInitializers) { if (protoProps) defineProperties(Constructor.prototype, protoProps, protoInitializers); if (staticProps) defineProperties(Constructor, staticProps, staticInitializers); return Constructor; }; })();

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    function _defineDecoratedPropertyDescriptor(target, key, descriptors) { var _descriptor = descriptors[key]; if (!_descriptor) return; var descriptor = {}; for (var _key in _descriptor) descriptor[_key] = _descriptor[_key]; descriptor.value = descriptor.initializer ? descriptor.initializer.call(target) : undefined; Object.defineProperty(target, key, descriptor); }

    return {
        setters: [function (_aureliaFramework) {
            bindable = _aureliaFramework.bindable;
        }, function (_aureliaRouter) {
            Router = _aureliaRouter.Router;
        }],
        execute: function () {
            NavBar = (function () {
                var _instanceInitializers = {};

                function NavBar() {
                    _classCallCheck(this, NavBar);

                    _defineDecoratedPropertyDescriptor(this, "router", _instanceInitializers);
                }

                _createDecoratedClass(NavBar, [{
                    key: "router",
                    decorators: [bindable],
                    initializer: function initializer() {
                        return null;
                    },
                    enumerable: true
                }], null, _instanceInitializers);

                return NavBar;
            })();

            _export("NavBar", NavBar);
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

使用gulp-babel方法有效,但我想它不一定是正确的选项,因为我不认为babel是为了转换Typescript所以我可能在以后编写一些高级代码时遇到问题.

问题

有什么我可以使用官方的Typescript转换器使其工作?

谁都成功了?

Mik*_*ham 7

请尝试模块:"amd"而不是模块:"system"