javascript - typescript中导入模块的方式有哪些以及require js在模块加载中的作用是什么

标签 javascript module typescript require

我是 typescript 新手,需要 js。我见过几种在 typescript 中导入模块的方法。但我不清楚下面的代码在做什么..

1./// <reference path="foo.ts" />
2.import {utils} from "./util";
3.require(["bar"],function(bar){
  //use module here
  });
4.define(["bar"],function(bar){
  //use module here
  });
5.requirejs(["bar"],function(bar){
  //use module here
  });

它们之间有何不同。请任何人帮忙。

最佳答案

  1. TypeScript-specific way of loading modules .
  2. Is the standard way of loading modules .
  3. 怎么样 RequireJS加载模块。

RequireJS 是一个在 JS 有加载模块的官方语法之前就存在的库。 RequireJS 模块是使用 define 调用来定义的,传入依赖项和一个在加载所有依赖项时运行的函数。 require 调用会启动加载过程,通常出现在应用程序的一开始。

import/export 是加载 JavaScript 模块的标准语法。 iirc 去年对语法进行了标准化。模块解析尚未标准化,目前也未由任何浏览器实现。但这种语法被不同的工具用来“编译”JS。

我无法解释太多 TypeScript 的内容。这篇文章应该为您提供所需的一切。

The /// directive is the most common of this group. It serves as a declaration of dependency between files.

关于javascript - typescript中导入模块的方式有哪些以及require js在模块加载中的作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937962/

相关文章:

typescript 错误 TS1005 : ';' expected (II)

javascript - 在 Angular JS 中更改文本的正确方法是什么?

facebook - 在 Node.js 中获取 Facebook 页面的公共(public)提要

typescript - 为什么 IntelliJ 在保存时不编译 TypeScript?

javascript - 带有 $http 注入(inject)的 angularjs 自定义模块

PythonAnywhere:导入 Mechanize 给出 ImportError,但 pip 给出 "Already satisfied"

javascript - 如何在 Global Jest 设置中查找别名?

javascript - 按名称删除 cookie?

Javascript forEach() 在 IE 中不起作用

javascript - 在包含许多已加载图像的 HTML 页面中,使用 CSS 显示/不透明度/可见性来隐藏它们在性能方面有区别吗?