作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在Electron中创建一个hello world项目,发现可以将Typescript用于Main进程https://electronjs.org/blog/typescript。
它说使用Typescript将文件扩展名从index.js更改为index.ts,然后更新package.json以指向新脚本:
{
"name": "electrontypescript",
"version": "1.0.0",
"description": "Typescript and Electron",
"main": "index.ts",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^5.0.1"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
const { TypeHouse } = require ("./TypeHouse");
function test() {
}
export class Cat {
}
export class TypeHouse {
public status: String = "ready";
private _output: String = "";
readonly startTime = Date.now();
private running: Boolean = false;
constructor(private _message: String, private _prompt: String) {
this.setStatus(_message);
}
async execute(): Promise<void> {
try {
//await CommandExecutor.execute(this);
} catch (exception) {
this.handleError(exception);
} finally {
//this.emit("end");
}
}
handleError(message: TypeHouse | string): void {
this.setStatus("Status.Failed");
if (message) {
//
}
}
isRunning(): boolean {
return this.running !== false;
}
public setStatus(value: String) {
this._output = value;
}
}
module.exports = {TypeHouse, Cat};
{
"name": "electron-app",
"version": "1.0.0",
"description": "Electron",
"main": "index.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^5.0.1",
"typescript": "^3.5.1"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
App threw an error during load Error: Cannot find module './TypeHouse' Require stack: - /Users/projects/ElectronApp/index.ts - /Users/projects/ElectronApp/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js
最佳答案
Electron提供了类型声明,而不提供直接运行TypeScript的能力。在运行之前,我们仍然需要将TypeScript转换为JavaScript。
main
指向index.js
。 npm start
。 npm install -g typescript
tsc --init // create a tsconfig.json file with reasonable default values
tsc // transpile your TypeScript to JavaScript
npm start // run the output index.js file
Hmm... where do you put the npm run build? Do I replace the value in the start property? I've updated the post with package.json
{
"name": "electron-app",
"version": "1.0.0",
"description": "Electron",
"main": "index.js",
"scripts": {
"build": "tsc", <--------------------------
"start": "electron ."
},
"devDependencies": {
"electron": "^5.0.1",
"typescript": "^3.5.1"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
npm run build
,与执行
./node_modules/.bin/tsc
等效。
关于typescript - 如何在Electron项目中添加自己的 typescript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498489/
我是一名优秀的程序员,十分优秀!