在 Angular2 应用程序中使用 GeoFire

2023-12-01

我正在尝试在我的 Angular2 (RC5) 应用程序中设置 GeoFire。我已经使用 npm 安装了 geofire 和 firebase 并配置了 systemjs 来导入它。这是我的 package.json:

{
  "name": "MyProject",
  "version": "0.1.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "^2.0.0-rc.5",
    "@angular/compiler": "^2.0.0-rc.5",
    "@angular/core": "^2.0.0-rc.5",
    "@angular/forms": "0.3.0",
    "@angular/http": "2.0.0-rc.5",
    "@angular/platform-browser": "^2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "^2.0.0-rc.5",
    "@angular/router": "3.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.5",
    "angular2-in-memory-web-api": "0.0.15",
    "bootstrap": "^3.3.6",
    "core-js": "^2.4.0",
    "firebase": "^3.3.0",
    "geofire": "^4.1.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings": "^1.0.4"
  }
}

和我的 systemjs 配置文件:

  var map = {
    'app':                        'app', // 'dist',
    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs',
    'firebase':                   'node_modules/firebase',
    'geofire':                    'node_modules/geofire',
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    'firebase':                   { main: 'firebase.js', defaultExtension: 'js' },
    'geofire':                    { main: 'dist/geofire.js', defaultExtension: 'js' },
  };

最后,我尝试在 app.component.ts 文件中导入 geofire,如下所示:

import * as GeoFire from "geofire";

但是在运行 npm start 来转换并启动服务器时,出现以下错误:

➜  MyProject npm start

> [email protected] start /Users/max/Development/myproject
> tsc && concurrently "npm run tsc:w" "npm run lite"

app/app.component.ts(3,26): error TS2307: Cannot find module 'geofire'.

npm ERR! Darwin 15.5.0
npm ERR! argv "/usr/local/Cellar/node/5.10.1/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v5.10.1
npm ERR! npm  v3.8.3
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `tsc && concurrently "npm run tsc:w" "npm run lite" `
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] start script 'tsc && concurrently "npm run tsc:w" "npm run lite" '.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the MyProject package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     tsc && concurrently "npm run tsc:w" "npm run lite"
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs MyProject
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls MyProject
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/max/Development/MyProject/npm-debug.log

这是我的 tsconfig:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  }
}

其余的项目文件基于此处找到的 angular2 快速入门:

https://angular.io/guide/quickstart

有人知道如何让 tsc 找到模块并成功转译吗?

谢谢。最大限度。


该快速入门教程缺少一个重要点:SystemJS 和 TypeScript 是两个独立的工具,每个工具都有自己的配置。

将 geofire 添加到 systemjs 配置对 typescript 模块分辨率没有影响。看到这个导入后

import * as GeoFire from "geofire";

tsc 尝试在通常可以找到 javascript 模块类型的地方查找 geofire.ts,并给出错误,因为它不在那里。

有三种不同的方法可以解决这个问题。

任何状况之下,geofiresystemjs.config.js 中的包配置必须将其格式声明为global, 像这样:

'geofire': {
    main: 'dist/geofire.js', 
    defaultExtension: 'js', 
    meta: {'dist/geofire.js': {format: 'global'}} 
 },

第一个解决方案是使用环境模块为 geofire 自己创建尽可能简单的类型声明。

Create geofire.d.ts应用程序文件夹中的文件:

declare namespace geofire {
    function GeoFire(firebaseRef: any): void;
}

declare module 'geofire' {
    export = geofire;
}

这足以编译 import 语句以及调用类型检查:

   var geoFire = new GeoFire(firebaseRef);

Typescript 2.0 的注释

对于 typescript 2.0,上面的代码不起作用。您必须使用特定于打字稿的import = syntax

import GeoFire = require('geofire');

并更改定义geofire.d.ts to:

declare module 'geofire' {
    function GeoFire(firebaseRef: any): void;
    export = GeoFire;
}

解决这个问题的另一种可能的方法是使用plugin对于使用 TypeScript API 调用 TypeScript 并挂钩其模块解析的 SystemJS,以便考虑 SystemJS 配置。使用该插件,该导入将在浏览器中运行时运行,但从命令行调用 tsc 仍会给出相同的错误。

第三种方法是使用 SystemJS API 导入 geofire,而不是 app.component.ts 中的 import 语句:

import { Component } from '@angular/core';

declare var SystemJS : any;
var GeoFirePromise = SystemJS.import('geofire');

@Component({
  selector: 'my-app',
  template: '<h1>My First Angular 2 App</h1>'
})

export class AppComponent {

    constructor() {  // or wherever you need to use geofire

        GeoFirePromise.then(function(GeoFire: any) {
            console.log(typeof GeoFire);
        });
    }

}

注意SystemJS.import返回一个必须加载 geofire.js 的承诺,所以无论你想在哪里使用它,你都必须这样做

    GeoFirePromise.then(function(GeoFire: any) {

如果这不方便,你可以声明全局 GeoFire 变量

declare var GeoFire: any;

并使用 HTML 中的 script 标签加载 geofire.js,如中所述离子论坛上的这篇文章。然后,您就不需要将 geofire 添加到 systemjs.config.js 中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Angular2 应用程序中使用 GeoFire 的相关文章

随机推荐

  • 为什么IE8是IE7模式。变量“IE”== 7

    我的 HTML 页面中有如下标题 我在 IE8 中打开该网站 当我在 Web Developer 中查看它时 如果 IE 7 处于活动状态 WebBrowser 在 IE8 紧凑视图 上设置 BrowserMode 在 IE7 标准 上设置
  • Facebook FQL 获取 Facebook 页面的粉丝列表[重复]

    这个问题在这里已经有答案了 可能的重复 查询 喜欢 我的 Facebook 页面的用户 目前该网站上的所有解决方案都不起作用 我是 Facebook 页面的管理员 想导出所有粉丝的 ID 这可能吗 https api facebook co
  • Mysql - CAST 的唯一约束(TIMESTAMP as DATE)

    我有一个TIMESTAMP myDate我的 MYSQL 表中的字段 有没有办法可以定义一个唯一的字符串stringForTheDay对于每一个day in myDate 就像是 UNIQUE stringForTheDay day in
  • SVG 膨胀/侵蚀滤镜与 Illustrator 偏移路径

    下面是在 Chrome 22 0 1229 79 Mac 上呈现的 SVG 图像的屏幕截图 原始的 svg 位于 jsfiddle 上 http jsfiddle net LGBk5 左图是使用 SVG s 扩张和腐蚀滤镜制作的 右图是使用
  • Node.js Web 服务器中的并行请求

    如果我有一个运行 Node js 的 Web 服务器 那么我是否能够同时处理多个请求 从我的初步测试中我可以看出 Node 主要是单线程的 目前只能处理一个 HTTP 请求 但如果一个请求需要很长时间才能完成 例如上传大量数据 则所有其他请
  • 如果我不编辑代码,PHP Mail 就会停止工作

    我的 PHP 邮件有问题 它有时会停止工作 我什至没有接触代码 我有一个脚本可以检查所需的表单是否为空 这个脚本有效 但是 如果我填写所有必填字段 我应该会收到一条文本 显示 您的邮件已成功发送 但我没有 但是 如果我通过仅将一些文本向下移
  • C# 实体 LINQ 返回错误且重复的值

    我使用以下代码 List
  • cakephp 2 ajax 表单

    我在 cakephp 2 中构建 ajax 表单时遇到问题 它显然自 1 3 以来已经发生了很大变化 我正在使用以下代码 div div div div
  • C 中的通用二叉搜索树

    我已经实现了二叉搜索树 但我也想使其通用 代码如下 typedef struct treeNode int data struct treeNode left struct treeNode right treeNode 和功能 treeN
  • laravel with() 方法与 load() 方法

    我真的很努力去理解两者之间的区别with 方法和load 方法 但没能真正理解 据我所知 使用with 方法 更好 因为我渴望加载关系 看来如果我使用load 我加载关系就像使用hasMany 或与对象之间的关系相关的任何其他方法 我理解错
  • 传递到字典中的模型项是 .. 类型,但是该字典需要类型为 的模型项

    添加了此问题和社区维基答案 以帮助解决许多未解答的问题 如中讨论的这个元帖子 我有一些代码 当它执行时 它会抛出一个异常 传递到字典中的模型项是 Bar 类型 但该字典需要 Foo 类型的模型项 这是什么意思 我该如何解决 该错误意味着您正
  • 单个查询中的 EF 多个聚合

    我想根据不同的条件获取一组的计数 var invoices new AccountingEntities Transactions var c1 invoices Count i gt i Type 0 var c2 invoices Co
  • Opera PreventDefault() on keydown 事件

    我正在尝试在我的网络应用程序中嵌入一些按键绑定 但我在 Opera 上遇到了困难 我有这个代码 window onkeydown function e var key e keyCode e keyCode e charCode e cha
  • 当我编织时,Rstudio 正在删除关键文件(PDF 和 HTML)

    所以我正在做一场 R 噩梦 我已经返回到在 RStudio 的上一次迭代 或可能更多 下构建的项目 我制作了一份可行的报告 并要求我更新 而我当前的烦恼那时并不存在 发生的情况如下 My report file is ISS Time Se
  • 批处理:搜索字符串以跳过上面的行并将结果写入新文件

    我已经成功编写了一个脚本 它需要一个字符串在特定文件中搜索 然后输出它第一次出现的行 然后我将该值放入 for 循环中并跳过解析该行数并写入它内容到一个新文件 然而 我没有得到空行 我发现这很难解决 我正在搜索的字符串是 缓存它出现的行号
  • java.lang.NoSuchFieldError:VERSION_2_3_0

    我刚刚将 Struts2 版本升级到 2 5 2 然后开始出现错误 我的应用程序甚至无法启动 我对 pom xml 和 web xml 进行了更改 我不确定我是否错过了任何课程 但日志没有这么说 pom xml
  • 如何使用 .NET 从现有 VFP (OLEDB) 表创建新的 VFP (OLEDB) 表?

    我们有一个创建许多 Visual Foxpro DBF 表的应用程序 每个表都有不同的架构 但它们都包含一个已知的日期字段 我被要求创建另一个应用程序 在 C 中 它将上周的数据从每个表复制到一个新表 在与源表不同的文件夹中 不同的表将保留
  • 如何使用 C# 在 ListBox 中创建多于 2 列?

    如何在 ListBox 中创建超过 2 列 以及如何插入数据 使用 C 提前致谢 Use UseCustomTabOffsets and CustomTabOffsets如下面的 VB NET 示例所示 Public Class Form1
  • printf(0, "%d", num) 中的 0 有什么作用?

    我通常用 C 编写代码 但我正在用 C 开发一个项目 我遇到了具有以下语法的 printf printf 0 d n num 我环顾四周 找不到 printf 中第一个 0 的作用的解释 有人可以向我解释一下吗 谢谢 Because xv6
  • 在 Angular2 应用程序中使用 GeoFire

    我正在尝试在我的 Angular2 RC5 应用程序中设置 GeoFire 我已经使用 npm 安装了 geofire 和 firebase 并配置了 systemjs 来导入它 这是我的 package json name MyProje