这个将是最后一篇关于小程序的记录了,课程接近尾声,最后一个是关于用户的page页面,看看这个页面中有哪些值得学习的地方!

一、page中my开发

这个主要是展示用户喜欢的杂志,以及用户的信息,需要创建两个相关的组件,一个是like-button组件,这个作用是来美化按钮,一个是preview组件,这个是用来展示标记为喜欢的杂志的,先把页面page中的my相关的代码研究一下

1、my.wxml文件

 <view class="container">
<image class="bg" src="/images/my/my@bg.png"></image>
<!-- <open-data type="userAvatarUrl" class="avatar avatar-position"></open-data> -->
<v-button wx:if="{{!authorized}}" class="avatar-position" bind:getuserinfo="onGetUserInfo" open-type="getUserInfo">
<image class="avatar" slot="img" src="/images/my/my.png"></image>
</v-button>
<view wx:if="{{authorized}}" class="avatar-container avatar-position">
<image src="{{userInfo.avatarUrl}}" class="avatar"></image>
<text>{{userInfo.nickName}}</text>
</view>
<view class="about-container">
<view class="about-us" bindtap="onJumpToAbout">
<image src="/images/my/about.png"></image>
<text class="description">关于我们</text>
</view>
<view class="about-us">
<text class="book_num">{{bookCount}}</text>
<text class="description">喜欢的书</text>
</view>
</view>
<!-- 喜欢杂志列表 -->
<view class="like-container">
<image class=".headline" src="/images/my/like.png" />
<view class="preview-container">
<block wx:for="{{classics}}" wx:key="">
<v-preview bind:tap="onPreviewTap" class="preview" classic="{{item}}" />
</block>
</view>
</view>
</view>
<!-- <button open-type="getUserInfo" bindgetuserinfo="getUserInfo">授权</button> --> <image bindtap="onStudy" class="study" src="/images/my/study.png"></image>

这里需要注意的是如何获取用户信息的代码,需要用户进行授权,但是触发用户授权的弹窗是能是button按钮,这里的处理方法是自定义的封装了一种button按钮,这就是下面这段代码

   <v-button wx:if="{{!authorized}}" class="avatar-position" bind:getuserinfo="onGetUserInfo" open-type="getUserInfo">
<image class="avatar" slot="img" src="/images/my/my.png"></image>
</v-button>

在封装image-button的时候,使用了slot这种插槽,为了提高组件的通用性,具体的使用方法,不详细讲解了

2、my.js文件代码

 import {
BookModel
} from '../../models/book.js'; import {
ClassicModel
}from '../../models/classic.js'
// 实例化
const bookModel = new BookModel();
const classicModel = new ClassicModel(); Page({ /**
* 页面的初始数据
*/
data: {
authorized:false,
userInfo:null,
bookCount:0,
classics:null
}, /**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// wx.getUserInfo({
// success:data=>{
// console.log(data);
// }
// })
this.userAuthorized();
this.getMyBookCount();
this.getMyFavor();
},
// 获取书籍的喜欢的总数
getMyBookCount(){
bookModel.getMyBookCount().then(res => {
this.setData({
bookCount:res.count
})
})
}, // 获取喜欢杂志的列表
getMyFavor(){
classicModel.getMyFavor(res=>{
this.setData({
classics:res
})
})
}, // 判断用户是否授权
userAuthorized(){
wx.getSetting({
success:data=>{
if(data.authSetting['scope.userInfo']){
wx.getUserInfo({
success:data=>{
// console.log(data)
this.setData({
authorized:true,
userInfo:data.userInfo
})
}
})
}else{
this.setData({
authorized:false,
userInfo:null
})
}
}
})
}, // 获取用户信息
onGetUserInfo(event){
const userInfo = event.detail.userInfo;
if(userInfo){
this.setData({
userInfo: userInfo,
authorized: true
})
}
}, onJumpToAbout(event){
wx.navigateTo({
url: '/pages/about/about',
})
}, onStudy(event){
wx.navigateTo({
url: '/pages/course/course',
})
},

js中逻辑也比较简单,唯一需要注意的地方就是获取用户信息的那个地方,需要查看一下微信小程序的官方文档来进行开发,不然你自己也不知道如何调用微信官方的接口来获取用户的数据,pages中还有两个page页面,一个是about一个是course,这两个就比较简单了,至于样式css代码,这里就不贴出来了

二、image-button组件的开发

1、index.wxml相关代码

 // index.wxml代码
<button bindgetuserinfo="onGetUserInfo" open-type="{{openType}}" class="container" plain="{{true}}">
<slot name="img"></slot>
</button>

2、index.js代码

 // components/image-button/index.js
Component({
options:{
multipleSlots: true
},
/**
* 组件的属性列表
*/
properties: {
openType:{
type:String
}
}, /**
* 组件的初始数据
*/
data: { }, /**
* 组件的方法列表
*/
methods: {
onGetUserInfo(event){
this.triggerEvent('getuserinfo',event.detail,{});
}
}
})

3、index.wxss样式

 .container{
padding: 0 !important;
border: none !important;
}

这个需要注意的是multipleSlots这个参数要设置成true

三、preview组件的开发

1、index.wxml页面代码

 <view catch:tap="onTap" class="container">
<view class="head">
<v-tag text="{{typeText}}" tag-class="tag" />
<v-like class="like" read-only="{{true}}" like="{{true}}" count="{{classic.fav_nums}}" />
</view>
<image class="{{classic.type==200?'music-img':'other-img'}}" src="{{classic.image}}"></image>
<view class="text">{{classic.content}}</view>
</view>

2、index.js代码

 // components/preview/index.js
Component({
/**
* 组件的属性列表
*/
properties: {
classic: {
type: Object,
observer: function(newVal) {
if (newVal) {
var typeText = {
100: "电影",
200: "音乐",
300: "句子"
}[newVal.type]
}
this.setData({
typeText: typeText
})
}
}
}, /**
* 组件的初始数据
*/
data: {
typeText:String
}, /**
* 组件的方法列表
*/
methods: {
onTap:function(event){
// 注意catchtap与bindtap的区别
this.triggerEvent('tap',{
cid:this.properties.classic.id,
type:this.properties.classic.type
},{})
}
}
})

3、index.wxss样式代码

 .container{
display: flex;
flex-direction: column;
align-items: center;
width:330rpx;
background-color: #ffffff;
} .head{
display: flex;
width:100%;
flex-direction: row;
align-items: center;
justify-content: space-between;
height:80rpx;
} .tag{
margin-left:20rpx;
margin-top:-2rpx;
height:40rpx;
width:72rpx ;
font-size:24rpx;
background-color:#f7f7f7 !important;
} .like{
margin-top:4rpx;
margin-right:4rpx;
} .other-img{
width:100%;
height:240rpx;
} .music-img{
border-radius: 50%;
width:240rpx;
height:240rpx;
} .text{
padding:30rpx;
font-size:28rpx;
height:130rpx;
color:#666666;
overflow: hidden;
}

这样的话,基本上整个page页面基本上就完后了,小程序的总体开发也基本上完成了,觉得这个项目一路学下来,收获最多不是学会了一点小程序开发的知识,而是对代码格式,代码风格的改善,我觉得这也是算对代码的一种敬畏之心吧!

内容出处:七月老师《纯正商业级小程序开发》视频课程

微信小程序之组件的集合(六)的更多相关文章

  1. 微信小程序之组件的集合(四)

    这个主要是来开发book的这个大模块的,看看如何优雅的开发出booked模块! 一.book模块的创建 这个就很简单了,创建一个大的框架是很简单的 二.组件的编写 (1)wxml组件页面的编码 首先是 ...

  2. 微信小程序之组件的集合(三)

    看看音乐播放组件是如何实现完成的音乐的播放的!!! 一.音乐music组件的开发 1.页面以及页面样式的开发 // music组件页面开发 <view hidden="{{hidden ...

  3. 微信小程序之组件的集合(二)

    继续微信小程序开发的学习,继续跟着老师的讲课思路来学习,继续开发项目中所用到的组件 一.导航栏navi组件的开发 1.新建组件的文件结构 这个就是先新建目录navi.然后在navi文件夹中新建comp ...

  4. 微信小程序之组件的集合(五)

    这个是学习复杂的组件的封装的,在课程中,主要实现的是书单上方的搜索功能组件的开发,这个应该是较之前的组件是有一定难度的,但是现在学到现在,感觉前端的内容和后端的内容比较起来,还是比较容易的,而且好多内 ...

  5. 微信小程序之组件的集合(一)

    小程序中是很强调组件中开发的,我们看到的页面是由许多组件组成的,但是这些组件是如何组合在一起的呢?来学习一下!  一.组件中数据的获取 接着上一篇文章,继续来优化代码,如何把从服务器上获取的数据显示到 ...

  6. 微信小程序把玩(二十六)navigator组件

    原文:微信小程序把玩(二十六)navigator组件 navigator跳转分为两个状态一种是关闭当前页面一种是不关闭当前页面.用redirect属性指定. 主要属性: wxml <naviga ...

  7. 微信小程序image组件binderror使用例子(对应html、js中的onerror)

    官方文档  binderror HandleEvent 当错误发生时,发布到 AppService 的事件名,事件对象event.detail = {errMsg: 'something wrong' ...

  8. 微信小程序倒计时组件开发

    今天给大家带来微信小程序倒计时组件具体开发步骤: 先来看下最终效果: git源:http://git.oschina.net/dotton/CountDown 分步骤-性子急的朋友,可以直接看最后那段 ...

  9. 微信小程序input组件抖动及textarea组件光标错位解决方案

    问题一: 使用微信小程序input组件时,在移动端唤起focus或blur事件时,因光标占位导致内容出现叠影及抖动现象. 解决方案: 用<textarea>组件代替了<input/& ...

随机推荐

  1. Mysql 权限命令整理大全

    mysql show slave status 需要什么权限 grant replication client on *.* to 'user_name'@'%';

  2. 关于OpenLiveWriter出错的修补方法

    OpenLiveWriter使用一段时间后可能会打不开,提示错误如下: 这是只需要把电脑的.net更新到4.6以上版本就可以了.

  3. Ubuntu 更新错误修复大全

    合并列表问题 当你在终端中运行更新命令时,你可能会碰到这个错误“合并列表错误”,就像下面这样: E:Encountered a section with no Package: header, E:P ...

  4. 如何在select标签中使用a标签跳转页面

    1. 需求: 在select中想直接使用a标签跳转,错误想法 <select id=""> <option>choose one</option> ...

  5. PAT甲级——A1124 Raffle for Weibo Followers

    John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...

  6. 第三周课堂笔记1thand2thand3th

    元组   元组是以逗号隔开的 元组有索引有切片,元组是小括号和中括号的集合, 元组中的东西不可修改(小括号内的东西不可被修改,但是小括号里的列表和字典可以被修改)   2. 由内存地址来分 可变数据类 ...

  7. 面试系列24 dubbo负载均衡策略和集群容错策略

    (1)dubbo负载均衡策略 1)random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重 ...

  8. 菲波那切数列(Fibonacci Number)

    什么是菲波那切数列?自己google一下,面试题里面经常遇到,考试递归算法用的. 在菲波那切数列中用递归不太好.第三种算法最好. 第一 递归算法最差了,不想说.测试一下,当N=6000时,半天出不来数 ...

  9. SpringCloud学习笔记《---02 Eureka ---》篇

  10. 用 Docker 搭建Sha--dow--sock--s 笔记

    首先得找一台海外服务器,该服务器一定要在海外. 一.在https://my.vultr.com/购买一台海外服务器,亲测选美国Miami速度最稳: 二.系统我选了CentOS 7 x64,在CentO ...