简体   繁体   English

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

[英]Unable to switch from JS to TS in ionic 2

I changed the file extensions of my app files including app/app.js 我更改了app文件的文件扩展名,包括app / app.js

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

It doesn't tell me which file I should be looking at to fix the error. 它没有告诉我应该查看哪个文件来修复错误。

I tried looking with git grep and read a bit about Angular2's entry points to find out where it is loaded, but no luck yet. 我尝试用git grep查看并阅读一些关于Angular2的入口点,以找出它的加载位置,但还没有运气。

Using TypeScript into an Ionic2 project isn't just switching extensions of files ;-) 将TypeScript用于Ionic2项目不仅仅是切换文件的扩展名;-)

You need to integrate the TypeScript compiler into Gulp with the gulp-tsc' plugin: 您需要使用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/'))
});

This page could help you: 此页面可以帮助您:

Thierry Templier put me on the right track, and here is the answer. Thierry Templier让我走上正轨,这就是答案。

Documentation in case anyone else is wondering. 以防其他人想知道的文件。

Ionic2 is different than ionic1 basically uses browserify now. Ionic2与ionic1不同,现在基本上使用browserify。 To switch from ES6 to TS, you would need to setup typscirpt for your project and set the dependencies like so: 要从ES6切换到TS,您需要为项目设置typscirpt并设置依赖关系,如下所示:

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

    3.1 npm install typings --global 3.1 npm install typings --global

    3.2 tsd query node --action install --save 3.2 tsd query node --action install --save

  4. Make sure you have the file your_app/typings/main.d.ts 确保你有文件your_app/typings/main.d.ts
    with the content: /// <reference path="main/ambient/es6-shim/index.d.ts" /> 内容: /// <reference path="main/ambient/es6-shim/index.d.ts" />

  5. lastly the tsconfig.json file 最后是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 } }

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

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