阿修罗
阿修罗
  • 发布:2024-05-18 20:41
  • 更新:2024-05-18 20:41
  • 阅读:100

uniapp x main.js 增加读取网络

分类:uni-app x

main.uts 文件内容=====================================================
import App from './App.uvue'

import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
app.use(function (app) {
app.config.globalProperties.http = (url:string) => {
return new Promise((resolve, reject) => {
uni.request<UTSJSONObject>({
url: 'https://www.wwww.cn/api_v2.' + url + '.html',
method:'POST',
// data: params,
// header: reqHeader,
success: (res) => {
console.log(res)
resolve(res.data as UTSJSONObject)
},
fail(err) {
uni.showModal({
title: '数据对接失败' + err.errCode,
showCancel:false
});
},
complete: () => {
uni.hideLoading();
}
});
})
};
});

return {  
    app  
}  

}
main.uts 文件内容=====================================================

页面调用=======================
methods: {
getMain(){
getApp().http('api').then( res =>{
this.getmain = res
})
}
}
页面调用=======================

web预览 未出现错误,可正常接收api数据

APP真机运行报错:

20:40:07.422 [plugin:uni:app-uts] 编译失败
20:40:07.422 ‌error: 类型不匹配: 推断类型是UTSPromise<TypeVariable(T)>,但预期的是Unit。‌
20:40:07.422 at main.uts:11:14
20:40:07.422 9 | app.use(function (app) {
20:40:07.423 10 | app.config.globalProperties.http = (url:string) => {
20:40:07.423 11 | return new Promise((resolve, reject) => {
20:40:07.423 | ^
20:40:07.423 12 | uni.request<UTSJSONObject>({
20:40:07.423 13 | url: 'https://jz.hulianhujia.cn/api_v2.' + url + '.html',
20:40:07.423 ‌error: 类型不匹配: 推断类型是UTSPromise<UTSJSONObject>,但预期的是Unit。‌
20:40:07.423 at main.uts:11:14
20:40:07.423 9 | app.use(function (app) {
20:40:07.423 10 | app.config.globalProperties.http = (url:string) => {
20:40:07.423 11 | return new Promise((resolve, reject) => {
20:40:07.424 | ^
20:40:07.424 12 | uni.request<UTSJSONObject>({
20:40:07.424 13 | url: 'https://jz.hulianhujia.cn/api_v2.' + url + '.html',
20:40:07.424 ‌error: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: ‌
20:40:07.424 ‌public fun VueComponent.http(url: String): Unit defined in uni.UNIEEA7E68 in file index.kt‌
20:40:07.424 at pages/sign.uvue:51:13
20:40:07.424 49 | },
20:40:07.424 50 | getMain(){
20:40:07.424 51 | getApp().http('api').then( res =>{
20:40:07.425 | ^
20:40:07.425 52 | this.getmain = res
20:40:07.425 53 | })
20:40:07.425 ‌error: Cannot infer a type for this parameter. Please specify it explicitly.‌
20:40:07.425 at pages/sign.uvue:51:31
20:40:07.425 49 | },
20:40:07.425 50 | getMain(){
20:40:07.425 51 | getApp().http('api').then( res =>{
20:40:07.425 | ^
20:40:07.425 52 | this.getmain = res
20:40:07.425 53 | })

2024-05-18 20:41 负责人:无 分享
已邀请:

要回复问题请先登录注册