如何在StencilJS中发出http请求(GET / POST / PUT / DELETE)?
我尝试使用axios,如下所示:npm install axios --save和模具组件import axios from 'axios';。一旦我调用axios.get(...),就会收到以下错误消息:
错误绑定:节点_模块/axios/lib/适配器/http.js,行:4
模块不能导入自身。
L3: var utils =需要量(‘././utils’);
L4: var =需要量(‘./../core/ settle’);
L5: var buildURL =buildURL(‘./../helpers/buildURL’);
我知道这可能与这个问题有关:https://github.com/ionic-team/stencil/issues/98
但是,对于如何使html请求在模具组件中工作,有什么建议吗?
发布于 2017-11-14 09:37:00
我们可以使用fetch API。它是浏览器本机,因此不需要导入。StencilJS也为它提供了一个多填充,所以它在任何地方都能工作。
感谢@insanicae为我指点它。
示例:
import { Component, State } from '@stencil/core';
@Component({
tag: 'app-profile',
styleUrl: 'app-profile.css'
})
export class AppProfile {
@State() name: string;
componentWillLoad() {
fetch('https://api.github.com/users/ErvinLlojku')
.then((response: Response) => response.json())
.then(response => {
this.name = response['name'];
});
}
render() {
return [
<ion-header>
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-back-button defaultHref="/" />
</ion-buttons>
<ion-title>Profile: {this.name}</ion-title>
</ion-toolbar>
</ion-header>,
<ion-content padding>
<p>My name is {this.name}.</p>
</ion-content>
];
}
}索取更多信息,请查阅官方文件。API接口
发布于 2017-11-16 14:07:29
您甚至可以使用我的组件使用Fetch API,只需删除URL并侦听OK或KO事件。
https://stackoverflow.com/questions/47281015
复制相似问题