Angular Service using third party packages

0

I am creating a service in angular that consumes a package to query a api , but I always get the same error;

  

Can not resolve all parameters for ApiService: (?).

o Code:

import { Injectable, Inject } from '@angular/core';
import { WooCommerceAPI } from 'woocommerce-api';

@Injectable()
export class ApiService {
  woocommerce: any;

  constructor(@Inject(WooCommerceAPI) woocommerce: WooCommerceAPI) {
    this.woocommerce = woocommerce({
    url: 'http://wc-project.dev/',
    consumerKey: 'ck_123',
    consumerSecret: 'cs_123',
    wpAPI: true,
    version: 'wc/v2'
  });
}

  getProducts(): any {
    return this.woocommerce.get('products');
  }
}

I've tried using OnInit but I get the error

  

this.woocommerce is undefined

Because the ngOnInit function is started even before the attribute declaration until I understand this error.

But how can I use the api package in my service ?

    
asked by anonymous 08.08.2017 / 17:59

1 answer

1

As the API is not part of the Angular you need to import everything with * and associate directly with a variable at import time.

You also do not need to put inside the constructor since it does not have any provider.

import { Injectable, Inject } from '@angular/core';
import * as WooCommerceAPI from 'woocommerce-api';

@Injectable()
export class ApiService {
  woocommerce: any;

  constructor() {
    this.woocommerce = WooCommerceAPI ({
    url: 'http://wc-project.dev/',
    consumerKey: 'ck_123',
    consumerSecret: 'cs_123',
    wpAPI: true,
    version: 'wc/v2'
  });
}

  getProducts(): any {
    return this.woocommerce.get('products');
  }
}
    
11.08.2017 / 15:10