【问题标题】:Unable to switch from JS to TS in ionic 2无法在 ionic 2 中从 JS 切换到 TS
【发布时间】:2016-08-19 05:26:08
【问题描述】:

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

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

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

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

【问题讨论】:

  • 从 es5/es6 切换到 typescript 的最佳方式是开始一个新项目并导入所有文件。 $ ionic start MyIonic2Project 教程 --v2 --ts

标签: javascript typescript angular ionic2


【解决方案1】:

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

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

var typescript = require('gulp-tsc');

(...)

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

此页面可以帮助您:

【讨论】:

  • 我在 ionic 2 中的任何地方都看不到这些脚本。我查看了一个新的 ionic 项目,其中包含 ts。另外,我认为不用安装gulp-tsc就可以自动检测的原因是因为即使你用js构建一个ionic 2项目,文件里面的脚本也几乎是一样的。
  • 我的回答中提供的代码必须在gulpfile.js 文件中。是的,几乎一样 ;-) 因为 TypeScript 是 ES6 的超集,但存在一些差异。这个问题可以帮助你:stackoverflow.com/questions/36820243/….
【解决方案2】:

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

文档以防其他人想知道。

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

  1. gulpfile.jsvar buildBrowserify = require('ionic-gulp-browserify-es2015');
    var buildBrowserify = require('ionic-gulp-browserify-typescript');
  2. package.jsonionic-gulp-browserify-es2015": "^1.0.2"
    对于"ionic-gulp-browserify-typescript": "^1.0.0"
  3. Install typescript

    3.1npm install typings --global

    3.2tsd 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 } }

【讨论】:

    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 2016-10-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多