Aurelia + 打字稿 + @bindable

Aurelia + Typescript + @bindable

我开始学习 Aurelia,我正在按照他们网站上的 Getting Started 指南进行操作,但我使用的不是 Javascript,而是 Typescript。一切都很好,但我在 @bindable 装饰器在 nav-bar 组件中工作时遇到问题。

我的设置如下:

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

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

export class NavBar {
      @bindable router: Router = null;
}

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

{
   "compilerOptions": {
      "noImplicitAny": false,
      "noEmitOnError": false,
      "removeComments": false,
      "target": "es5",
      "module": "system",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true
   }
}

如果我让 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);
        }
    }
});

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

如果我改用 gulp-babel,生成的导航-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);
        }
    };
});

使用 gulp-babel 方法可行,但我想这不一定是正确的选择,因为我不认为 babel 是用来转换 Typescript 的,所以我稍后在编写一些高级代码时可能会遇到问题。

问题

有什么办法可以让它使用官方的 Typescript 转译器工作吗?

有人成功了吗?

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