【问题标题】:Imports not working in Angular 2 application导入在 Angular 2 应用程序中不起作用
【发布时间】:2023-03-24 05:01:01
【问题描述】:

当我说导入不起作用时,我指的是一种特定类型的导入:来自@types,格式为import * as _ from 'lodash'。但是,我可以通过这样做来解决这个问题:import _ from 'lodash'。到目前为止我一直在做的事情。

这在此线程中有很好的记录:Importing lodash into angular2 + typescript application,其中许多不同的人发布了一个导入是否有效而另一个无效。这个问题是关于确定为什么会出现这种情况。

到目前为止,这还不是问题,但现在我正在尝试使用https://github.com/angular/angularfire2 包,它已经成为问题,因为我现在必须编辑节点包并替换:import * as firebase from 'firebase'; import firebase from 'firebase',否则它不知道 'firebase' 是什么。

如果我在 ./node_modules/angularfire2/auth/firebase_sdk_auth_backend.js 中记录从 firebase 端返回的对象,我会得到:

如果我将其切换到import firebase from 'firebase',应用程序会加载并且日志是:

我一直在搞乱我的tsconfig.json,但无济于事。即通过将 es2015 更改为 ES6 并打开和关闭标志。它的当前(故障)状态是(我假设问题就在这里,尽管我不确定):

  {
    "compilerOptions": {
      "allowSyntheticDefaultImports": true,
      "declaration": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "lib": [
        "dom", "es2015"
      ],
      "module": "es2015",
      "moduleResolution": "node",
      "sourceMap": true,
      "target": "es5"
    },
    "include": ["src/**/*.ts"],
    "exclude": ["node_modules"],
    "compileOnSave": false,
    "atom": {
      "rewriteTsconfig": false
    }
  }

npm -v (4.0.2) tsc -v (版本 2.0.10) ionic -v (2.1.13) 如果您需要任何其他软件包版本,我会将它们发布在下面的 cmets 中。

看来我需要能够import * as foo from 'bar' => 我需要做什么才能做到这一点?

注意 这也是一个 ionic 2 应用程序(但我认为这没有太大区别)

【问题讨论】:

    标签: angular typescript import ionic2


    【解决方案1】:

    据我所知,这是由于您的 @angular/compiler-cli 版本所致。如果你将它撞到2.1.1 它允许导入 * 作为来自 'firebase' 类型的 firebase。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多