Angular Service using third-party Packages

Asked

Viewed 70 times

0

I’m creating a service in angular who consumes a package to consult a api, but I always get the same mistake;

Can’t resolve all Parameters for Apiservice: (?).

the 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 with OnInit but I get the bug

this.Woocommerce is Undefined

as the function ngOnInit is started even before the declaration of attributes until I understand this error.

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

1 answer

1

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

You also don’t need to put inside the constructor since it doesn’t 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');
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.