由于我是做后端的,学习的时候一直好奇怎么和c#一样创建类-方法,找到的时候废了一番精力,希望能够帮助到你
1.先创建一个ArkTS文件
2.代码
import { http } from '@kit.NetworkKit' import { JSON } from '@kit.ArkTS'; import { LanguageDto } from '../pages/Dtos/LanguageDto'; //类,注意需要 export export class HttpService {
//方法 extraData: object | string | null 表示 extraData 可以是 object,string ,null 这三种类型 static Get<T>(url: string, extraData: object | string | null, callback: (data: T) => void) { let httRequest = http.createHttp(); let address = 'http://192.168.1.4:6084';
//获取选中语言 const langModel = AppStorage.get<LanguageDto>("LanguageModel"); httRequest.request(address + url, { method: http.RequestMethod.GET, header: { "LanguageId": langModel?.Id } }, (a, e) => { const parsedJson = JSON.parse(e.result.toString()) const result = parsedJson as HttpResult<T> if (result.Code == 200) { callback(result.Data) } }); } }
标签:Arkts,http,string,创建,result,import,const,null,方法 From: https://www.cnblogs.com/manga/p/18365385