首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stenciljs中的http请求

stenciljs中的http请求
EN

Stack Overflow用户
提问于 2017-11-14 08:33:32
回答 2查看 6K关注 0票数 7

如何在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请求在模具组件中工作,有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-14 09:37:00

我们可以使用fetch API。它是浏览器本机,因此不需要导入。StencilJS也为它提供了一个多填充,所以它在任何地方都能工作。

感谢@insanicae为我指点它。

示例:

代码语言:javascript
复制
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接口

票数 14
EN

Stack Overflow用户

发布于 2017-11-16 14:07:29

您甚至可以使用我的组件使用Fetch API,只需删除URL并侦听OK或KO事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47281015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档