stenciljs 中的 http 请求
http requests in stenciljs
如何在 StencilJS 中发出 http 请求(GET / POST / PUT / DELETE)?
我尝试按如下方式使用 axios:npm install axios --save
和模板组件 import axios from 'axios';
。我一调用 axios.get(...)
就收到以下错误消息:
[错误] 捆绑:node_modules/axios/lib/adapters/http.js,行:4
模块无法导入自身
L3: var utils = require('./../utils');
L4: var settle = require('./../core/settle');
L5: var buildURL = require('./../helpers/buildURL');
我知道这可能与这个问题有关:https://github.com/ionic-team/stencil/issues/98
但是,关于如何在模板组件中获取 html 请求的任何建议?
我们可以使用fetch
API。它是浏览器原生的,因此不需要导入。 StencilJS 也有一个 polyfill,所以它无处不在。
感谢@insanicae 指点我。
Example:
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>
];
}
}
更多信息请查阅fetch官方文档。 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
您甚至可以使用我的 component 来使用 Fetch API,只需放下 URL 并监听 OK 或 KO 事件。
如何在 StencilJS 中发出 http 请求(GET / POST / PUT / DELETE)?
我尝试按如下方式使用 axios:npm install axios --save
和模板组件 import axios from 'axios';
。我一调用 axios.get(...)
就收到以下错误消息:
[错误] 捆绑:node_modules/axios/lib/adapters/http.js,行:4
模块无法导入自身
L3: var utils = require('./../utils');
L4: var settle = require('./../core/settle');
L5: var buildURL = require('./../helpers/buildURL');
我知道这可能与这个问题有关:https://github.com/ionic-team/stencil/issues/98
但是,关于如何在模板组件中获取 html 请求的任何建议?
我们可以使用fetch
API。它是浏览器原生的,因此不需要导入。 StencilJS 也有一个 polyfill,所以它无处不在。
感谢@insanicae 指点我。
Example:
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>
];
}
}
更多信息请查阅fetch官方文档。 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
您甚至可以使用我的 component 来使用 Fetch API,只需放下 URL 并监听 OK 或 KO 事件。