在export和之间的Typescript有什么区别default export.在所有教程中,我看到人们在export他们的课程中,如果我default在导出之前没有添加关键字,我就无法编译我的代码.
另外,我在官方打字稿文档中找不到任何默认导出关键字的痕迹.
export class MyClass {
collection = [1,2,3];
}
Run Code Online (Sandbox Code Playgroud)
不编译.但:
export default class MyClass {
collection = [1,2,3];
}
Run Code Online (Sandbox Code Playgroud)
请问.
错误是: error TS1192: Module '"src/app/MyClass"' has no default export.
我有一个lib用ES6编写的node.js库(用Babel编译),我在其中导出以下子模块:
"use strict";
import * as _config from './config';
import * as _db from './db';
import * as _storage from './storage';
export var config = _config;
export var db = _db;
export var storage = _storage;
Run Code Online (Sandbox Code Playgroud)
如果从我的主项目中我包含这样的库
import * as lib from 'lib';
console.log(lib);
Run Code Online (Sandbox Code Playgroud)
我可以看到正确的输出,它按预期工作{ config: ... }.但是,如果我尝试像这样包含库:
import lib from 'lib';
console.log(lib);
Run Code Online (Sandbox Code Playgroud)
它会undefined.
有人能解释一下这里发生了什么吗?这两种导入方法不应该是等价的吗?如果没有,我错过了什么区别?
我通过添加此属性更改了tsconfig.json
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
为了能够导入 npm 包 import * as ms from "ms";
但我仍然收到这个错误
This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
更新:
如果我更改为import ms from "ms",那么它可以与编译器一起正常工作,但不能与 VSCode linter 一起使用,并且错误是
can only be default-imported using the 'allowSyntheticDefaultImports' flagts(1259)
index.d.ts(25, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' …Run Code Online (Sandbox Code Playgroud) 两者有什么区别
export * as bar from 'foo'
Run Code Online (Sandbox Code Playgroud)
和
export { default as bar } from 'foo'
Run Code Online (Sandbox Code Playgroud)
在我的具体情况下,我尝试了以下两种方法,它们都有效,想知道它们之间的根本区别。
// echarts v5.0.0
export * as ECharts from 'echarts/lib/echarts.js'
export { default as ECharts } from 'echarts/lib/echarts.js'
Run Code Online (Sandbox Code Playgroud)
babel.config.js
module.exports = {
// "@vue/cli-plugin-babel": "~4.5.0",
presets: ['@vue/cli-plugin-babel/preset'],
}
Run Code Online (Sandbox Code Playgroud)