聚合体组件中的http调用与angularjs服务的比较。

问题描述 投票:2回答:1

我有一个服务,在我的应用程序中被不同的web组件使用。所以我想创建一个web组件,它将拥有我的服务,并且可以在任何需要的地方导入我的web组件,就像在angularjs中一样。

在Angular

app.service('myService', function () {
    this.hello = function () {
        return "Hello World";
    };
    this.getData= function() {
      return $http({
        method: 'JSON', 
        url: 'SomeURL'
      });
}

在聚合物

<dom-module id="my-service">
    <script>

    var myService= new Object();

    myService.hello = function () {
        return "Hello World";
    };

    myService.getData= function() {
      //How to convert this part in polymer context or we need to use <iron-ajax> , if yes then how?
      });
    }
</script>

谁能告诉我什么是最好的方式来实现同样的聚合物。

任何帮助将被高度赞赏!

javascript angularjs polymer polymer-1.0
1个回答
0
投票

我发现的一种方式来实现服务(聚合物2文档)是:

Axios .Axios docs Axios docs

<script>

class MyService extends Polymer.Element {
  static get is() { return 'my-service; }
  static get properties() { url: 'my/service-url' }

  getEndpoint(id) {
    axios.get(this.url, {
      params: {
       ID: id
      }
    })
   .then(function (response) {
     console.log(response);
    })
  }

  postEndpoint(params) {
    //Use axios to post data from backend
  }
}

customElements.define(MyService.is, MyService);
</script>

希望对你也有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.