前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Angular Component TypeScript代码和最后转换生成的JavaScript代码比较

Angular Component TypeScript代码和最后转换生成的JavaScript代码比较

作者头像
Jerry Wang
发布2020-08-14 09:49:39
5610
发布2020-08-14 09:49:39
举报

TypeScript代码使用@Component定义一个Component:

@Component({
  selector: 'app-shipping',
  templateUrl: './shipping.component.html',
  styleUrls: ['./shipping.component.css']
})

转换后的JavaScript代码:

var ShippingComponent = /** @class */
        (function() {
            function ShippingComponent(cartService, http1, http2) {
                this.cartService = cartService;
                this.http1 = http1;
                this.http2 = http2;
            }
            ShippingComponent.prototype.ngOnInit = function() {
                // return this.http.get('/assets/shipping.json');
                this.shippingCosts = this.cartService.getShippingPrices();
            }
            ;
            var _a, _b, _c;
            ShippingComponent = __decorate([core_1.Component({
                selector: 'app-shipping',
                template: require("./shipping.component.html"),
                styles: [require("./shipping.component.css")]
            }), __metadata("design:paramtypes", [typeof (_a = typeof cart_service_1.CartService !== "undefined" && cart_service_1.CartService) === "function" ? _a : Object, typeof (_b = typeof http_1.HttpClientModule !== "undefined" && http_1.HttpClientModule) === "function" ? _b : Object, typeof (_c = typeof http_2.HttpClient !== "undefined" && http_2.HttpClient) === "function" ? _c : Object])], ShippingComponent);
            return ShippingComponent;
        }());

分别对应了下图红色和蓝色区域:

(1) TypeScript里的构造函数,对应JavaScript里的ShippingComponent构造函数。

(2) TypeScript里的ngOnInit Hook,对应JavaScript里的原型链上的ShippingComponent.prototype.ngOnInit.

(3) TypeScript里的@Component注解(或者叫装饰器decorator),对应JavaScript变量__decorate指向的函数。

最后,从@angular/core导入的core_1.Component, 经过装饰器函数__decorate处理之后,返回ShippingComponent,被赋给exports的ShippingComponent属性,这样其他Component就可以通过import导入这个Component了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档