无法在离子 2 中从 JS 切换到 TS

Unable to switch from JS to TS in ionic 2

本文关键字:JS TS 中从      更新时间:2023-09-26

我更改了我的应用程序文件的文件扩展名,包括应用程序/应用程序.js

Error: Cannot find module 'app/app.js' from 'app_folder'

它没有告诉我应该查看哪个文件来修复错误。

我试着用git grep查找并阅读了一些关于 Angular2 的入口点以找出它的加载位置,但还没有运气。

在 Ionic2 项目中使用 TypeScript 不仅仅是切换文件的扩展名 ;-)

您需要将 TypeScript 编译器与 gulp-tsc' 插件集成到 Gulp 中:

var typescript = require('gulp-tsc');
(...)
gulp.task('compile', function() {
  gulp.src(paths.src)
    .pipe(typescript({
      emitError: false
    }))
    .pipe(gulp.dest('www/js/'))
});

此页面可以帮助您:

  • https://github.com/driftyco/ionic-typescript-example

Thierry Templier让我走上了正确的轨道,这就是答案。

文档,以防其他人想知道。

Ionic2与ionic1不同,现在基本上使用browserify。要从 ES6 切换到 TS,您需要为您的项目设置 typscirpt 并像这样设置依赖项:

  1. gulpfile.js改变var buildBrowserify = require('ionic-gulp-browserify-es2015');
    对于var buildBrowserify = require('ionic-gulp-browserify-typescript');
  2. package.json改变ionic-gulp-browserify-es2015": "^1.0.2"
    对于"ionic-gulp-browserify-typescript": "^1.0.0"
  3. 安装打字稿

    3.1 npm install typings --global

    3.2 tsd query node --action install --save

  4. 确保您有文件your_app/typings/main.d.ts
    内容:/// <reference path="main/ambient/es6-shim/index.d.ts" />

  5. 最后是tsconfig.json文件

    tsconfig{ "compilerOptions": { "target": "es5", "module": "commonjs", "emitDecoratorMetadata": true, "experimentalDecorators": true }, "filesGlob": [ "**/*.ts", "!node_modules/**/*" ], "exclude": [ "node_modules", "typings/main", "typings/main.d.ts" ], "compileOnSave": false, "atom": { "rewriteTsconfig": false } }