【问题标题】:Ionic 2 - 'import' and 'export' may appear only with 'sourceType: module'Ionic 2 - 'import' 和 'export' 可能只与 'sourceType: module' 一起出现
【发布时间】:2016-07-26 02:13:02
【问题描述】:

使用全新安装的 ionic2 处理新项目,安装 angular2-jwt 后出现此错误:

ParseError: 'import' and 'export' may appear only with 'sourceType: module'

D:\desenv\arquivos\workspace_inbit\medipop-parent\medipop-app\node_modules\angular2-jwt\angular2-jwt.ts:1
import {Injectable, Injector} from 'angular2/core';

复制:

ionic start testapp --v2 --ts 
cd testapp
npm i --save angular2-jwt

和应用页面:

@App({
    templateUrl: 'build/app.html',
    providers: [
        provide(AuthHttp, {
            useFactory: (http) => {
                return new AuthHttp(new AuthConfig({
                    headerPrefix: '',
                    noJwtError: true
                }), http);
            },
            deps: [Http]
        })
    ]
})
class MyApp {}

有人知道如何解决这个小谜题吗?

【问题讨论】:

  • 我在这里也遇到了同样的问题。你找到解决办法了吗?
  • 还没有 Michel,basarat 建议在 tsconfig 中包含一个module,但已经指定了一个:'module': 'commonjs'@Michael

标签: cordova typescript ionic-framework angular jwt


【解决方案1】:

我今天在 Ionic 2 beta 11 上遇到了同样的问题,对我来说,这是因为 (.js) 扩展文件 (user-data.js) 而不是 (.ts) 文件 (user-data.ts) ,所以将 .js 文件扩展名更改为 .ts 解决了我的问题。

【讨论】:

    【解决方案2】:

    尝试将此行添加到您的 ts 配置中:

    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "module"
    },
    

    如果这不起作用,请尝试在我的计算机上运行此配置 (tsconfig.json):

    {
        "compilerOptions": {
            "noImplicitAny": true,
            "module": "commonjs",
            "target": "ES5",
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true,
            "sourceMap": true,
            "declaration": true,
            "moduleResolution":"node"
        },
        "files": [
            "angular2-jwt.ts",
            "typings/browser.d.ts",
            "custom.d.ts"
        ]
    }
    

    【讨论】:

      【解决方案3】:

      'import' 和 'export' 可能只与 'sourceType: module' 一起出现

      您需要在 tsconfig 或命令行中指定 module。例如

      'module': 'commonjs'
      

      更多

      https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

      【讨论】:

      • 但是具体的模块是什么? @basarat
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2017-02-23
      • 2017-05-16
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多