与 angularjs 服务相比,聚合物组件中的 http 调用
http calls in polymer component as compared to angularjs services
我有一项服务被我的应用程序中的不同网络组件使用。所以我正在考虑创建一个 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>
谁能告诉我在 Polymer 中实现相同目标的最佳方法是什么。
任何帮助将不胜感激!!
我发现实现服务 (Polymer 2 docs) 的方法之一是:
Axios : 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>
希望对你也有帮助!!
我有一项服务被我的应用程序中的不同网络组件使用。所以我正在考虑创建一个 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>
谁能告诉我在 Polymer 中实现相同目标的最佳方法是什么。
任何帮助将不胜感激!!
我发现实现服务 (Polymer 2 docs) 的方法之一是:
Axios : 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>
希望对你也有帮助!!