Uniapp项目模板

项目目录架构:

request.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export default {
//GET请求
get(url, data = {}, header = {}) {
return this.request('GET', "http://127.0.0.1:8601"+url, data, header);
},

//POST请求
post(url, data = {}, header = {}) {
return this.request('POST', "http://127.0.0.1:8601"+url, data, header);
},

//封装uni.request
request(method, url, data, header) {
return new Promise((resolve, reject) => {
uni.request({
url: url,
method: method,
data: data,
header: header,
success: (res) => {
// 这里可以对响应结果进行预处理
if (res.statusCode === 200) {
resolve(res.data);
} else {
// 这里可以根据实际情况处理错误
reject(res);
}
},
fail: (err) => {
// 这里处理请求失败
reject(err);
}
});
});
}
}

修改下http的链接就行,这是向后端发送请求的封装

使用封装类例子

  • GET(后端使用@RequestParam接收):
1
2
3
request.get('/seat/getList?hall='+hall).then(res =>{
list.value = res.data
})
  • GET(后端使用@PathVariable接收):
1
2
3
request.get('/seat/getList/'+hall).then(res =>{
list.value = res.data
})
  • POST(后端使用@RequestBody接收):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const form = ref({
"line": '',
"vertical": '',
"hall": ''
})

function submit(){
request.post('/seat/addHall',form.value).then(res =>{
if(res === "ok"){
toast.show('添加成功')
}else{
toast.show('添加失败,或许已经有重复数据了?')
}
})

}

vue.config.js

1
2
3
4
5
6
7
8
9
10
module.exports = {
// 配置路径别名
configureWebpack: {
devServer: {
// 调试时允许内网穿透,让外网的人访问到本地调试的H5页面
disableHostCheck: true
}
}
// productionSourceMap: false,
}

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}

// #endif

就是HBuilder默认创建的

pages.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"easycom": {
"autoscan": true,
"custom": {
"^wd-(.*)": "wot-design-uni/components/wd-$1/wd-$1.vue"
}
},
"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "uni-app",
"enablePullDownRefresh": true
}
},
{
"path": "pages/index/test",
"style": {
"navigationBarTitleText": "uni-app2"
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "uni-app",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
},
"uniIdRouter": {}
}

第一个 easycom是引入的一个组件库:Wot Design Uni | 一个基于Vue3+TS开发的uni-app组件库,提供70+高质量组件,支持暗黑模式、国际化和自定义主题。 (wot-design-uni.cn)

其他的就是页面路由了,路由跳转:uni.navigateTo({url:”/pages/index/test”})

总结

一般会vue3就会uniapp了,反正我一天就可以上手写项目了.