stenciljs中的http请求

And*_*rni 5 xmlhttprequest axios stenciljs

如何在StencilJS中发出http请求(GET/POST/PUT/DELETE)?

我尝试使用axios如下:npm install axios --save和模板组件一样import axios from 'axios';.我一打电话就axios.get(...)收到以下错误信息:


[ERROR] bundling:node_modules/axios/lib/adapters/http.js,line:4

模块无法导入自身

L3:var utils = require('./../ utils');

L4:var sett = require('./../ core/sett');

L5:var buildURL = require('./../ helpers/buildURL');


我知道这可能与此问题有关:https://github.com/ionic-team/stencil/issues/98

但是,有关如何获取html请求的任何建议都在模板组件中工作?

And*_*rni 10

我们可以使用fetchAPI.它是浏览器原生的,因此不需要导入.StencilJS也有一个polyfill,所以它适用于所有地方.

感谢@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>
    ];
  }
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查阅官方提取文档.https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API