2016-11-16 16 views
0

いくつかのアプリで使用する認証サービス用のnpmモジュールを作成したいと思います。 私は他のパッケージを見ましたが、大きな違いが見つかりませんでした。Angular2カスタムnpmパッケージ

私のモジュールセクション::

私Package.json:

{ 
"name": "myApi-services", 
"version": "1.0.0-11", 
"main": "index.js", //referring to the compiled js file from index.ts 
"dependencies": { 
"@angular/common": "2.1.0", 
"@angular/core": "^2.1.0", 
"@angular/http": "^2.1.0", 
"@angular/platform-browser": "^2.1.0", 
"angular2-jwt": "^0.1.25", 
"rxjs": "5.0.0-beta.12", 
"zone.js": "^0.6.21" 
} 
} 

私Index.ts

export { MyAuthService} from "./authenticate" 

私認証クラス

import { Http, Headers } from '@angular/http'; 
import { Config } from './config' 

export class Authenticate { 

constructor(private http:Http, private authUrl:string) { 
    this.authUrl = Config.authUrl; 
} 

public login(username:string, password:string): void { 
    let headers = new Headers(); 
    headers.append('Authorization', 'Basic ' + btoa(username + ':' + password)); 
    headers.append('Accept', 'application/json'); 
    this.http.post(this.authUrl, {}, { headers: headers }) 
     .subscribe(
      response => { 
       this.storeToken(response.json().token); 
      }, 
      err => console.log(err), 
      () => console.log('Request Complete') 
     ); 
} 

private storeToken(token: string):void{ 
    localStorage.setItem('apiservices_token', token); 
    console.log(localStorage.getItem('apiservices_token')) 
} 
} 

私のインポートセクション::

package.json

"depenencies":{ 
    "MyApi-services": "file:/// .... " 
} 

輸入作品は、モジュールがnode_modulesフォルダにimportesを取得

app.module.ts

import { Authenticate } from 'myApi-services' 
@NgModule({ 
    imports:[ Authenticate ] 
}) 

app.component.ts

ブラウザによってスローされたエラーの

一つは:

Uncaught Error: Unexpected value 'Authenticate' declared by the module 'AppModule' 

このエラーは、モジュールをimortingによって引き起こされます。それをプロバイダとして宣言しても動作しません。エラーは同じです。

誰もがangle2のための独自のカスタムプラグインを作成する方法を知っていて、それを正しく注入する方法はありますか?

constructor(
    private http:Http, 
    private authUrl:string) { 
    this.authUrl = Config.authUrl; 

}

私はクラスで直接それらを宣言したように、モジュールが働いた:

export class Authenticate { 
     http: Http; 
     authUrl:strin; 

     constructor(){ 
      console.log("constructor without params"); 
     } 
} 

angular2のNPMモジュール

+1

Providersでの認証ではなくインポートを使用するようにしてください。インポートは主にモジュール宣言用です。 –

+0

同じ実行をスローします。モジュールのコンストラクタでクラス変数を宣言することは問題になりますか? – marcel

答えて

0

問題は、私のモジュールのコンストラクタパラメータでしたコンストラクタのparamsが好きではないようです。

Furter(Michałが言ったように):モジュールは輸入業者ではなく、プロバイダーに置かなければなりません。

関連する問題