Skip to content

Commit d64380b

Browse files
committed
注册小程序和页面
1 parent 51f8b15 commit d64380b

File tree

5 files changed

+113
-54
lines changed

5 files changed

+113
-54
lines changed

app.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 注册一个小程序示例
2+
App({
3+
// 小程序初始化完成时
4+
onLaunch: function (options) {
5+
// 获取用户信息
6+
console.log(options)
7+
},
8+
// 小程序显示出来时
9+
onShow: function (options) {
10+
// 1.判断小程序的进入场景
11+
console.log(options)
12+
switch(options.scene) {
13+
case 1001:
14+
break;
15+
case 1005:
16+
break;
17+
}
18+
19+
// 2.获取用户的信息, 并且获取到用户信息之后, 将用户的信息传递给服务器
20+
wx.getUserInfo({
21+
success: function(res) {
22+
console.log(res)
23+
}
24+
})
25+
},
26+
// 小程序隐藏时
27+
onHide: function () {
28+
29+
},
30+
// 小程序产生一些错误
31+
onError: function (msg) {
32+
33+
},
34+
globalData: {
35+
name: 'coderwhy',
36+
age: 18
37+
}
38+
})

pages/home/home.js

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
// pages/home/home.js
2-
Page({
2+
// getApp()获取App()产生的示例对象
3+
// const app = getApp()
34

4-
/**
5-
* 页面的初始数据
6-
*/
7-
data: {
5+
// const name = app.globalData.name;
6+
// const age = app.globalData.age;
87

8+
// 注册一个页面
9+
// 页面也有自己的生命周期函数
10+
Page({
11+
// ------------ 2.初始化数据 -------------------
12+
data: {
13+
message: '哈哈哈',
14+
list: []
915
},
1016

11-
/**
12-
* 生命周期函数--监听页面加载
13-
*/
14-
onLoad: function (options) {
15-
17+
// ------------ 1.监听页面的生命周期函数 -------------------
18+
// 页面被加载出来
19+
onLoad() {
20+
console.log('onLoad')
21+
const _this = this;
22+
wx.request({
23+
url: 'http://123.207.32.32:8000/recommend',
24+
// 箭头函数中的this一层层向上找
25+
success: function(res) {
26+
console.log(res)
27+
const data = res.data.data.list;
28+
_this.setData({
29+
list: data
30+
})
31+
}
32+
})
1633
},
17-
18-
/**
19-
* 生命周期函数--监听页面初次渲染完成
20-
*/
21-
onReady: function () {
22-
34+
// 页面显示出来时
35+
onShow() {
36+
console.log('onShow')
2337
},
24-
25-
/**
26-
* 生命周期函数--监听页面显示
27-
*/
28-
onShow: function () {
29-
38+
// 页面初次渲染完成时
39+
onReady() {
40+
console.log('onReady')
3041
},
31-
32-
/**
33-
* 生命周期函数--监听页面隐藏
34-
*/
35-
onHide: function () {
36-
42+
// 当页面隐藏起来时
43+
onHide() {
44+
console.log('onHide')
3745
},
38-
39-
/**
40-
* 生命周期函数--监听页面卸载
41-
*/
42-
onUnload: function () {
43-
46+
onUnload() {
47+
console.log('onUnload')
4448
},
4549

46-
/**
47-
* 页面相关事件处理函数--监听用户下拉动作
48-
*/
49-
onPullDownRefresh: function () {
50-
50+
// ------------ 3.监听wxml中相关的一些事件 -------------------
51+
handleGetUserInfo(event) {
52+
console.log(event)
5153
},
52-
53-
/**
54-
* 页面上拉触底事件的处理函数
55-
*/
56-
onReachBottom: function () {
57-
54+
handleViewClick() {
55+
console.log('哈哈哈被点击了')
5856
},
5957

60-
/**
61-
* 用户点击右上角分享
62-
*/
63-
onShareAppMessage: function () {
64-
58+
// ------------ 4.其他事件 -------------------
59+
// 监听页面的滚动
60+
onPageScroll(obj) {
61+
// console.log(obj)
62+
},
63+
// 监听页面滚动到顶部
64+
onReachBottom() {
65+
console.log('页面滚动到顶部')
66+
},
67+
onPullDownRefresh() {
68+
console.log('下拉刷新的事件')
6569
}
66-
})
70+
})

pages/home/home.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"usingComponents": {}
2+
"usingComponents": {},
3+
"enablePullDownRefresh": true
34
}

pages/home/home.wxml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
<!--pages/home/home.wxml-->
22
<text class='title'>Hello World</text>
33
<button size='mini'>按钮</button>
4+
5+
<!-- 2.获取用户信息的方式 -->
6+
<button size='mini'
7+
open-type='getUserInfo'
8+
bindgetuserinfo='handleGetUserInfo'>
9+
获取授权
10+
</button>
11+
12+
<!-- 3.展示用户信息的方法 -->
13+
<open-data type="userNickName"></open-data>
14+
<open-data type="userAvatarUrl"></open-data>
15+
16+
<view bindtap='handleViewClick'>{{message}}</view>
17+
18+
<view wx:for="{{list}}">{{item.title}}</view>

project.config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"ignore": []
55
},
66
"setting": {
7-
"urlCheck": true,
7+
"urlCheck": false,
88
"es6": true,
99
"postcss": true,
1010
"minified": true,
1111
"newFeature": true,
12-
"autoAudits": false
12+
"autoAudits": false,
13+
"checkInvalidKey": true
1314
},
1415
"compileType": "miniprogram",
1516
"libVersion": "2.7.1",

0 commit comments

Comments
 (0)