如何使用 TypeScript 模块:在浏览器中使用 systemjs 进行系统导出

编程入门 行业动态 更新时间:2024-10-12 01:33:13
本文介绍了如何使用 TypeScript 模块:在浏览器中使用 systemjs 进行系统导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使用 systemjs,并且我已经使用 tsc 导出了此代码:

导出如下:

System.register("SolarPopup", ["ModalBackground", "constants"], function (exports_4, context_4) {严格使用";var __moduleName = context_4 &&context_4.id;var ModalBackground_1、constants_2、SolarPopup;返回 {二传手: [功能(ModalBackground_1_1){ModalBackground_1 = ModalBackground_1_1;},函数(常量_2_1){常量_2 = 常量_2_1;

解决方案

当你在 tsconfig.json

中有这两个选项时

"module": "system","outFile":"./build/solar-popup.js",

TypeScript 生成包含多个 System.register 调用的单个输出文件,每个调用都使用自己的名称注册一个模块:

 System.register("SolarPopup", ["ModalBackground", "constants"], function ...

SolarPopup"是这里的模块名称.SystemJS 将此类文件解释为 捆绑包,而不是模块.

导入 bundle 的结果是一个空对象,副作用是里面的所有模块都注册了,可以立即导入,无需获取.

因此,您需要额外导入才能从包中获取模块.试试这个:

System.import('../build/solar-popup.js').then(function() {System.import('SolarPopup').then(function(SolarPopup){控制台.dir(SolarPopup);});});

I’m trying to use systemjs, and I have exported this code with tsc: https://github/quantumjs/solar-popup/blob/master/package.json#L10

{
  "compilerOptions": {
    "module": "system",
    "target": "es5",
    "sourceMap": true,
    "outFile":"./build/solar-popup.js",
    "sourceRoot": "./src/SolarPopup.ts",
    "rootDir": "./src/"

  },
  "exclude": [
    "node_modules"
  ]
}

However then trying to consume it in the browser the imported object doesn't contain any of the exports. I think this is due to the solar-popup.js not containing any exports, just System.register calls

The export looks like this:

System.register("SolarPopup", ["ModalBackground", "constants"], function (exports_4, context_4) {
    "use strict";
    var __moduleName = context_4 && context_4.id;
    var ModalBackground_1, constants_2, SolarPopup;
    return {
        setters: [
            function (ModalBackground_1_1) {
                ModalBackground_1 = ModalBackground_1_1;
            },
            function (constants_2_1) {
                constants_2 = constants_2_1;

etc

解决方案

When you have these two options in tsconfig.json

"module": "system",
"outFile":"./build/solar-popup.js",

TypeScript generates single output file that contains several System.register calls, each registering a module with its own name:

 System.register("SolarPopup", ["ModalBackground", "constants"], function ...

"SolarPopup" is a module name here. SystemJS interprets such file as a bundle, not a module.

The result of importing a bundle is an empty object, and a side effect is that all modules inside are registered and are available immediately for importing without need to fetch them.

So you need an additional import to get a module from the bundle. Try this:

System.import('../build/solar-popup.js').then(function() {
    System.import('SolarPopup').then(function(SolarPopup) {
        console.dir(SolarPopup);
    });
});

这篇关于如何使用 TypeScript 模块:在浏览器中使用 systemjs 进行系统导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:04:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   模块   器中   系统   TypeScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!