gpt4 book ai didi

typescript - Aurelia + typescript + @bindable

转载 作者:搜寻专家 更新时间:2023-10-30 21:16:54 26 4
gpt4 key购买 nike

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

我的设置如下:

  • Visual Studio 2015(ASP.NET vNext 项目)
  • typescript 1.5.3
  • JSPM 0.16.0 测试版 7
  • SystemJS 0.18.9
  • Aurelia(框架:0.15.0; Bootstrap :0.16.0)

这是我的 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 结果是一样的。使用这两种解决方案,绑定(bind)不起作用并且导航栏为空。

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

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

问题

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

有人成功了吗?

最佳答案

请尝试使用 module: "amd"而不是 module: "system"

关于typescript - Aurelia + typescript + @bindable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32156334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com