简体   繁体   English

在Angular 2中使用第三方库(parse.com)

[英]Use third party library (parse.com) in Angular 2

I am learning Angular 2 and I followed the tutorials of Egghead already, but I am pretty new to everything concerning Angular. 我正在学习Angular 2,我已经遵循了Egghead的教程,但我对Angular的一切都很新。

Now I want to do something more advanced and start using Parse.com with Angular 2. Normally I would include the parse.com library in the index.html page via <script src="//www.parsecdn.com/js/parse-1.6.2.min.js"></script> , but I want to write a ParseService via Angular 2 that I can use to manage the backend. 现在我想做更高级的事情并开始使用Parse.com和Angular 2.通常我会在index.html页面中通过<script src="//www.parsecdn.com/js/parse-1.6.2.min.js"></script>包含parse.com库<script src="//www.parsecdn.com/js/parse-1.6.2.min.js"></script> ,但我想通过Angular 2编写一个ParseService,我可以使用它来管理后端。

I can't seem to find how to include and use Parse in the service I want to write. 我似乎无法找到如何在我想写的服务中包含和使用Parse。 This is the very basic code I want to use to test the import. 这是我想用来测试导入的非常基本的代码。

import {Injectable} from 'angular2/core';
import {Parse} from '.../...'; // <-- This is what I want to do

@Injectable()
export class ParseService {
    constructor() {
        console.log('Creating ParseService');

        Parse.initialize('', '');
    }
}

I need some kind of Import at the top of the page including Parse, but from where should I get the necessary library? 我需要在页面顶部包含某种导入,包括Parse,但是我应该从哪里获得必要的库? I already tried via npm but without success. 我已经尝试过npm但没有成功。 Anyone already tried this? 有人试过这个吗?

uksz was right. uksz是对的。 You has to first install the component by the command 您必须首先通过命令安装组件

npm install --save parse

After that you can import it as any other component by typing 之后,您可以通过键入将其作为任何其他组件导入

import {Parse} from 'parse';

For more info look at this link https://forum.ionicframework.com/t/how-to-require-xyz-in-ionic2-angular2/42042 欲了解更多信息,请访问此链接https://forum.ionicframework.com/t/how-to-require-xyz-in-ionic2-angular2/42042

Hope it helps;) 希望能帮助到你;)

UPDATED 更新

With new version of angular this approach stopped to work. 随着角度的新版本,这种方法停止工作。 Here is my new step by step: how to use Parse library in Angular2 这是我的新步骤:如何在Angular2中使用Parse库

  1. Install Parse component to the project 将Parse组件安装到项目中

     npm install parse --save 
  2. Install Parse types 安装Parse类型

     npm install @types/parse --save 
  3. import Parse module 导入Parse模块

     const Parse: any = require('parse'); 
  4. use Parse module 使用Parse模块

     Parse.initialize("key"); ... 

Enjoy it with intellisense;) 用intellisense享受吧;)

You can do that by using OpaqueToken in Angular2 你可以在Angular2中使用OpaqueToken来做到这一点

1 . 1 Create a Token that is used to find an instance as below in a separate ts file. 在单独的ts文件中创建用于在下面查找实例的令牌。

import { OpaqueToken } from '@angular/core'

export let name_of_The_Token = new OpaqueToken('name_Of_The_Window_Object');

2. In your App.module , you need to import and declare a variable that is the name of your window object which makes the Token as a angular2 service so that you can use properties, methods in that javascript file across your components. 2.App.module中 ,您需要导入并声明一个变量该变量是窗口对象的名称,它使Token成为angular2服务,以便您可以在组件中使用该javascript文件中的属性和方法。

import { name_of_The_Token } from '/* file_Path */';
declare let name_Of_The_Window_Object : any;  //below your import statements

Step 3: Inject it to providers array of your module. 第3步:将其注入模块的提供者数组。

{ provide : name_of_The_Token , useValue : name_Of_The_Window_Object }

Guidance to use this token in components 在组件中使用此令牌的指南

  1. Import the token just like any other service and @Inject from angular-core 像任何其他服务一样导入令牌,并从angular-core导入@Inject

      import { name_of_The_Token } from '/* file_Path */'; import { Inject } from '@angular/core'; 
  2. In constructor of the component 在组件的构造函数中

      constructor(@Inject( name_of_The_Token ) private _serviceObject : any ) 
  3. Any where in your component you can use the variables and methods of your javascript file as 组件中的任何位置都可以使用javascript文件的变量和方法

      this._serviceObject.method1() this._serviceObject.variable1 ..... 

Note : One drawback is that you will not get intellisense . 注意 :一个缺点是你不会得到intellisense

Overcoming it: If you are looking for intellisense you need to wrap the methods and variables inside an interface and use it in the type**(instead of any)** of your token as 克服它:如果你正在寻找intellisense,你需要将方法和变量包装在一个接口中 ,并在你的令牌的类型**(而不是任何)**中使用它

export interface myCustom {
      method1(args): return_Type;
      method2(args): void;
      .....
   }

What you need to do, is you need to download Parse library with: 您需要做什么,是否需要下载Parse库:

npm install parse

Then, you need to reference it in your import in the right way - you need to specify in which folder the parse.js file is placed at. 然后,您需要以正确的方式在导入中引用它 - 您需要指定parse.js文件放在哪个文件夹中。

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

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