我使用相对路径在 TypeScript 中导入了一个模块。
// index.ts
import {Widget} from './components/Widget';
Run Code Online (Sandbox Code Playgroud)
Webpack 给了我以下错误:
// index.ts
import {Widget} from './components/Widget';
Run Code Online (Sandbox Code Playgroud)
我的 webpack 配置文件非常基本,规则中有 ts-loader 并指向 index.ts 作为入口文件。
我在这里做错了什么?
附加信息:
项目文件夹结构:
c:\project
?? src
? ?? index.ts
? ?? components
? ?? Widget.ts
?? webpack.config.js
?? tsconfig.json
Run Code Online (Sandbox Code Playgroud)
网络包配置:
const path = require('path');
const config = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{ test: /\.tsx?$/, use: 'ts-loader' }
]
}
};
module.exports = config;
Run Code Online (Sandbox Code Playgroud)
配置文件
{ …Run Code Online (Sandbox Code Playgroud) 我有一个颜色的枚举。我希望在枚举类中添加一个辅助方法“ toRGB()”,该类将枚举的实例转换为RGB对象。作为一种优化,我希望将字典作为静态变量创建一次。但是,正确的语法似乎使我难以理解。
有人可以建议正确的方法吗?
from enum import Enum
class RGB:
def __init__(self, r, g, b):
pass
class Color(Enum):
RED = 0
GREEN = 1
__tbl = {
RED: RGB(1, 0, 0),
GREEN: RGB(0, 1, 0)
}
def toRGB(self):
return self.__class__.__tbl[self.value]
c = Color.RED
print(c.toRGB())
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "C:/Users/user/Desktop/test.py", line 20, in <module>
print(c.toRGB())
File "C:/Users/user/Desktop/test.py", line 17, in toRGB
return self.__class__.__tbl[self.value]
TypeError: 'Color' object does not support indexing
Run Code Online (Sandbox Code Playgroud)