简体   繁体   English

如何通过cmd创建Cordova和TypeScript项目

[英]How to create a Cordova and TypeScript project by cmd

I use Cordova to develop a mobile application, and I type cordova create MyApp in command-line to create a Cordova project. 我使用Cordova开发移动应用程序,并在命令行中键入cordova create MyApp来创建Cordova项目。

I can use JavaScript but I need use the TypeScript in my project. 我可以使用JavaScript,但是我需要在项目中使用TypeScript。

Please help me to create a Cordova project, powered by TypeScript without Visual Studio, or help me to add TypeScript to my Cordova project. 请帮助我创建一个由TypeScript支持而不使用Visual Studio的Cordova项目,或者帮助我将TypeScript添加到我的Cordova项目中。

Thank you so much. 非常感谢。

In essence (without getting into code and asset bundling) what you'll want to do is to 本质上 (无需考虑代码和资产捆绑),您要做的就是

  • write your "TypeScript" code in another file, for example index.ts and then compile with output to ...\\MyApp\\www\\js\\index.js , 在另一个文件(例如index.ts编写“ TypeScript”代码,然后将输出编译到...\\MyApp\\www\\js\\index.js

where MyApp is your Cordova app folder and index.js is the one referenced in your app's index.html ( <script type="text/javascript" src="js/index.js"></script> ). 其中MyApp是您的Cordova应用程序文件夹, index.js是您应用程序的index.html<script type="text/javascript" src="js/index.js"></script> )中引用的文件夹。

  • you need to install the command-line TypeScript compiler , which can be installed as a Node.js package. 您需要安装命令行TypeScript编译器 ,该编译器可以作为Node.js软件包安装。 Open the "Windows Command Prompt" or "Terminal" and type: 打开“ Windows命令提示符”或“终端”,然后键入:

npm install -g typescript

To see if TypeScript is installed, type tsc -v . 要查看是否已安装TypeScript,请键入tsc -v This should print a version number, so you'll see something like Version 2.7.2 . 这应该打印一个版本号,因此您将看到类似Version 2.7.2

  • next copy index.js to index.ts so your \\js\\ folder will contain both files 接下来index.js复制到index.ts这样您的\\js\\文件夹将同时包含两个文件

  • after you edit and save your TypeScript code in index.ts , use "Command Prompt" or "Terminal", change directory to the \\js\\ folder, then run tsc index.ts (this will compile index.ts and update index.js ) 在将TypeScript代码编辑并保存到index.ts ,使用“命令提示符”或“终端”,将目录更改为\\js\\文件夹,然后运行tsc index.ts (这将编译index.ts并更新index.js

Note: when building your Cordova app, you will not need the source .ts file 注意:在构建Cordova应用时,您不需要源.ts文件

This is a manual process (you'll have to do it every time you change your code), but I think it will get you started and it gives you an understanding of what is going on before you move to bundling, etc... 这是一个手动过程(每次更改代码时都必须这样做),但是我认为它可以帮助您入门,并且可以让您在进行捆绑等之前了解正在发生的事情。

Next steps to take into account: 接下来要考虑的步骤:

  • use an editor with TypeScript support (such as Visual Studio Code) 使用具有TypeScript支持的编辑器(例如Visual Studio Code)
  • use a dist folder so you have clear separation of source files 使用dist文件夹,以便您清楚地分离源文件
  • use tsconfig.json 使用tsconfig.json
  • use Webpack ( https://webpack.js.org/concepts/ ) 使用Webpack( https://webpack.js.org/concepts/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM