在Javascript网页包项目中转换为Typescript(如何导出模块)
我目前正试图开始将Javascript网页包项目转换为Typescript的过程,这样所有内容都可以很好地输入。然而,我的配置似乎无法识别我已更改为Typescript的一个文件 项目已编译,但我在运行时遇到以下错误:在Javascript网页包项目中转换为Typescript(如何导出模块),javascript,typescript,webpack,Javascript,Typescript,Webpack,我目前正试图开始将Javascript网页包项目转换为Typescript的过程,这样所有内容都可以很好地输入。然而,我的配置似乎无法识别我已更改为Typescript的一个文件 项目已编译,但我在运行时遇到以下错误: TypeError: Angle.cyclic3dAxis is not a function Angle是我用Typescript重写的(以前的Javascript)文件,由2个小的静态函数组成,格式如下: export class Angle { public st
TypeError: Angle.cyclic3dAxis is not a function
Angle是我用Typescript重写的(以前的Javascript)文件,由2个小的静态函数组成,格式如下:
export class Angle
{
public static cyclic3dAxis(i: number): number{ /* function defined here */ }
public static anotherFunction(): number{/*defined here*/}
}
原始Javascript文件(我没有编写,但可以使用),我将其替换为TS one:
define([],function()
{
var Angle = { };
Angle.cyclic3dAxis = function(i) { /* function defined here */ };
Angle.anotherFunction = function() { /* defined here */ };
return Angle;
});
这些函数包含相同的代码
webpack.config.json的相关部分:
/* some vars declared here truncated for brevity */
module.exports = function(env)
{
/* more stuff here */
resolve : {
alias: {
Cesium: path.resolve(__dirname, "scripts/Cesium/Cesium.js")
},
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules : [
{
test: /\.js$/,
exclude: /(Cesium|node_modules)/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['es2015'],
}
}
]
},
{
test: /\.tsx?$/,
exclude: /(Cesium|node_modules)/,
use: [
{
loader: 'awesome-typescript-loader'
}
]
},
/* and more */
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"sourceMap": true
}
}
和我的tsconfig.json:
/* some vars declared here truncated for brevity */
module.exports = function(env)
{
/* more stuff here */
resolve : {
alias: {
Cesium: path.resolve(__dirname, "scripts/Cesium/Cesium.js")
},
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules : [
{
test: /\.js$/,
exclude: /(Cesium|node_modules)/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['es2015'],
}
}
]
},
{
test: /\.tsx?$/,
exclude: /(Cesium|node_modules)/,
use: [
{
loader: 'awesome-typescript-loader'
}
]
},
/* and more */
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"sourceMap": true
}
}
整个项目相当大,因此如果我将“allowJs”设置为true,就会出现Javascript堆内存不足的错误
角度由项目中的其他Javascript文件引用,如下所示:
define([
'./Angle',
],
function(
Angle
}
{
/* example function call */
functionName = function(i) {
return Angle.cyclic3dAxis(i);
};
});
export function cyclic3dAxis(i: number): number{ /* function defined here */ }
export function anotherFunction(): number{/*defined here*/}
请让我知道我是否做错了什么,或者我需要补充什么。谢谢你的帮助 转换文件中导出的结构与原始JS文件中的导出不匹配。模块应如下所示:
define([
'./Angle',
],
function(
Angle
}
{
/* example function call */
functionName = function(i) {
return Angle.cyclic3dAxis(i);
};
});
export function cyclic3dAxis(i: number): number{ /* function defined here */ }
export function anotherFunction(): number{/*defined here*/}
或者,如果需要使用静态方法创建类,则应使用export=Angle导出该类代码>:
class Angle
{
public static cyclic3dAxis(i: number): number{ /* function defined here */ }
public static anotherFunction(): number{/*defined here*/}
}
export = Angle;
您能否演示如何在使用角度的模块中导入角度