gpt4 book ai didi

javascript - 如何在 ES6 类中定义类级别常量

转载 作者:行者123 更新时间:2023-12-05 04:10:52 25 4
gpt4 key购买 nike

<分区>

我们如何定义类级别常量并在静态和实例方法中访问它?

 class ExternalRequests{

const HEADERS = { "Accept": "application/json, text/plain", "Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}


static get(url){
return fetch(url, {method: 'get', HEADERS})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}

static post(url, data){
return fetch(url, {method: 'post', HEADERS, body: data})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}

static put(url, data){
return fetch(url, {method: 'put', HEADERS, body: data})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}

static delete(){
return fetch(url, {method: 'delete', HEADERS})
.catch(_ => {
throw new Error("network error");
})
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
});
}

}

export default ExternalRequests;

错误

ERROR in ./externalRequests.js
Module build failed: SyntaxError: Unexpected token (3:8)

1 | class ExternalRequests{
2 |
> 3 | const HEADERS = { "Accept": "application/json, text/plain", "Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com