javascript – 是否可以从Loopback为Angular2生成服务?

前端之家收集整理的这篇文章主要介绍了javascript – 是否可以从Loopback为Angular2生成服务?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是关于 AngularJS JavaScript SDK的文档

这个例子适用于Angular.可以使用命令生成Angular客户端库@H_403_3@

$lb-ng ../server/server.js js/lb-services.js

是否存在将Angular2与Loopback一起使用的简单方法?@H_403_3@

编辑@H_403_3@

我目前在这个主题上发现了什么.@H_403_3@

> Issue在loopback-sdk-angular Github存储库中进行讨论.
>实现示例:BaseResourceModel在BaseResource上进行了配对.
>另一种方式 – 使用从Angular到Angular2的upgrade,直到Loopback Angular 2 SDK正式实现.
>我为Angular 2 in fork of loopback-sdk-angular完成了alpha版本的代码生成器.@H_403_3@

编辑@H_403_3@

> Loopback-sdk-builder
(comment)@H_403_3@

解决方法

此时你可以使用loopback-sdk-angular和loopback-sdk-angular-cli包的fork.

的package.json:@H_403_3@

"devDependencies": {
  //...
  "loopback-sdk-angular": "github:qeti/loopback-sdk-angular#188-angular2-support","loopback-sdk-angular-cli": "github:qeti/loopback-sdk-angular-cli#37-angular2-support"
}

生成客户端代码(TypeScript):@H_403_3@

./node_modules/.bin/lb-ng ./server/server.js ./client/src/app/lb-services.ts -l angular2

用法示例:@H_403_3@

import {Component,Injectable} from 'angular2/core';
import {UserApi as UserService} from './lb-services';
import {HTTP_PROVIDERS} from 'angular2/http';

@Component({
  selector: 'my-app',providers: [UserService,HTTP_PROVIDERS],template: 'some template'
})

@Injectable()
export class AppComponent {

  private login: string;
  private password: string;

  constructor(protected user: UserService) {}

  onLogin() {
    let self = this;
    // Example 1
    this.user.login({
      username: self.login,password: self.password
    })
    .subscribe(res => {
      // some actions on login
      this.getData();
    });
  }

  onlogout() {
    // Example 2
    this.user.logout().subscribe(() => {
      // some actions on logout
    });
  }

  public getData() {
    // Example 3
    this.user.count().subscribe((response: any) => {
      let lastRow = response.count;

      let data = this.user
        // Example 4
        .find({
          offset: 0,limit: 100
        })
        .subscribe(function(response: any) {
          // Process response
        });
    });
  }
}

猜你在找的JavaScript相关文章