[英]How to use axios in visual studio code extensions with webview
I am using this sample to develop my plugin.我正在使用这个示例来开发我的插件。 And I want to fetch some remote data and display data in webview.
我想获取一些远程数据并在 webview 中显示数据。 My question is how to use axios or other libs in this environment.
我的问题是如何在此环境中使用 axios 或其他库。 Thanks.
谢谢。
https://github.com/Microsoft/vscode-extension-samples/tree/master/webview-sample https://github.com/Microsoft/vscode-extension-samples/tree/master/webview-sample
if you want get data from api, you can use request.如果你想从 api 获取数据,你可以使用请求。
npm install request --save
you can you axios
instead of request
npm install request --save
你可以用axios
代替request
import request = require('request');
const URL = 'https://jsonplaceholder.typicode.com/todos/1';
request({
headers: {}, // add here headers if you needed
uri: URL,
method: 'GET',
// bodyData will come here if method is POST
}, (err, res, body) => {
const data = JSON.parse(body);
console.log(data);
// here call other stuff wants to do.
});
Haven't used webview, but for a simple extension axios
is used like this, in extension.js
:没有使用过 webview,但是对于一个简单的扩展,
axios
在extension.js
中是这样使用的:
const vscode = require('vscode');
const axios = require('axios');
async function activate(context) {
try {
const res = await axios.get(
"https://example.com/",
{ headers: { "Accept-Encoding": "gzip,deflate,compress" } }
);
console.log(res.data);
} catch(e){ console.log(e) }
// ...
}
function deactivate() {}
module.exports = { activate, deactivate }
I hope a similar thing will work in webview.我希望类似的东西能在 webview 中工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.