1. Generate SDK in loopback application
The @mean-expert/loopback-sdk-builder is a community driven module forked from the officialloopback-sdk-angular
and refactored to support Angular 2.- npm install --save-dev @mean-expert/loopback-sdk-builder
Angular 2 for Web:
- $ ./node_modules/.bin/lb-sdk server/server.js ./sdk -d ng2web -i enabled
{
"scripts": {
"build:sdk": "./node_modules/.bin/lb-sdk server/server.js ./sdk -d ng2web -i enabled"
}
}
then:
$ cd to/loopback/project
$ npm run build:sdk
Please follow these all URL:
- https://loopback.io/doc/en/lb2/Angular-2-SDK.html
- https://github.com/mean-expert-official/loopback-sdk-builder
- https://github.com/mean-expert-official/loopback-sdk-builder/wiki/1.-Install-Builder-&-Build-SDK
2. User SDK In angular application.
1 .Set baseUrl and apiVersion these in environment file:
a. apiVersion => api version check in "config.json" loopback application.
export const environment = {
production: false,
baseUrl: 'http://localhost:3000',
apiVersion: 'api/v1'
};
2. Set SDKBrowserModule in main module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import {SDKBrowserModule} from './sdk/index';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
SDKBrowserModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
3. Set LoopBackConfig and LoopBackAuth in-app component
import { PostsApi } from './sdk/services/custom/Posts';
import { Component } from '@angular/core';
import { LoopBackConfig } from './sdk/lb.config';
import { LoopBackAuth } from './sdk/services/core/auth.service';
import { environment } from '../environments/environment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
constructor(private _postsApi: PostsApi) {
LoopBackConfig.setBaseURL(environment.baseUrl);
LoopBackConfig.setApiVersion(environment.apiVersion);
this._postsApi.find().subscribe(res => {
console.log('sucess>>', res);
}, err => {
console.log('err>>>>', err);
});
}
}
Please follow these all URL:
- https://loopback.io/doc/en/lb2/Versioning-your-API.html
- https://github.com/mean-expert-official/loopback-sdk-builder/wiki/5.-Usage-Examples
No comments:
Post a Comment