简体   繁体   English

使用npm的twitter模块的angular2

[英]angular2 using twitter module from npm

I had problem with LODASH importing to Angular2 app, got answer on this post here and all working now. 我在将LODASH导入Angular2应用程序时遇到问题, 在此处获得此帖子的答案并且现在都可以使用。

Next problem I have is that I do not know how to make "twitter" npm module to work within the app. 我遇到的下一个问题是,我不知道如何使“ twitter” npm模块在应用程序中工作。

Now I installed "npm install twitter" and tried to import it with: 现在,我安装了“ npm install twitter”并尝试将其导入:

import * as Twitter from 'twitter';

but I get "cannot find module 'twitter'. I tried adding to system.config without luck. 但是我得到“找不到模块'twitter'。我尝试添加到system.config中没有运气。

What am I doing wrong here? 我在这里做错了什么? How should the code look like for import and using this twitter module? 导入和使用此twitter模块的代码应如何显示?

Here is system.config in index.html: 这是index.html中的system.config:

  System.config({
    packages: {
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    map: {
      lodash: 'node_modules/lodash/lodash.js',
      twitter: 'node_modules/twitter/index.js'
    },
    meta: {
      lodash: { format: 'amd' },
      twitter: { format: 'amd' }
    }
  });

I believe you have to add the definition file of the twitter module to a typings folder. 我相信您必须将twitter模块的定义文件添加到typeings文件夹中。 Or install it using tsd. 或使用tsd安装它。 After that you can use it in your typescript code using a reference: 之后,您可以使用参考在打字稿代码中使用它:

download manually 手动下载

  1. Download the definition file from here 此处下载定义文件
  2. Add it to a typings folder in your source root 将其添加到源根目录中的“ typesings”文件夹中

or 要么

install using tsd 使用tsd安装

  1. Open cmd in source folder 在源文件夹中打开cmd
  2. npm install tsd -g
  3. tsd install twitter --save

After that add the reference in the top of your typescript file 之后,在您的打字稿文件顶部添加引用

/// <reference path="../typings/twitter.d.ts"/>

The twitter module is a commonjs module, so it won't work with amd. twitter模块是commonjs模块,因此不适用于amd。 Try the following config to see if it works: 尝试以下配置以查看其是否有效:

System.config({
  packages: {
  app: {
    format: 'register',
    defaultExtension: 'js'
  }
},
map: {
  lodash: 'node_modules/lodash/lodash.js',
  twitter: 'node_modules/twitter'
},
meta: {
  lodash: { format: 'commonjs' },
  twitter: { format: 'commonjs' }
}
});

try the import like this: 尝试像这样的导入:

import * as Twitter from 'twitter/index';

Does it work? 它行得通吗? I don't think lodash is an amd module as well, its also commonjs. 我不认为lodash也是一个amd模块,它也不是commonjs。 You probably can remove the full meta section, systemJs will auto-detect the type of module system successfully most of the times. 您可能可以删除完整的meta部分,systemJ会在大多数情况下成功地自动检测模块系统的类型。

I don't think you could use this module in such scenario as its a server-side module and needs Node running. 我认为您不能在服务器端模块这种情况下使用此模块,并且需要运行Node。 I've managed to implement twitter authentication using other module: https://github.com/jublonet/codebird-js 我已经设法使用其他模块实现Twitter身份验证: https : //github.com/jublonet/codebird-js

I've been working for a while on a simple project implementing social media logins. 我已经在一个简单的项目上工作了一段时间,该项目实现了社交媒体登录。 If you want you can take a look at: https://github.com/bkulov/EventOrgApp 如果您愿意,可以看一下: https : //github.com/bkulov/EventOrgApp

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

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