这个将是最后一篇关于小程序的记录了,课程接近尾声,最后一个是关于用户的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. vsftp 被动模式配置

    直接复制粘切过来就能用 这里只讲下配置,安装方法可以直接yum 配置文件修改 anonymous_enable=NO #关闭匿名用户 xferlog_file=/var/log/vsftpd.log ...

  2. Flask框架图

  3. 概率dp——期望水题hdu4405

    还是逆推,如果遇到跳板直接继承目标地的期望即可 #include<bits/stdc++.h> using namespace std; #define maxn 200005 doubl ...

  4. kubernetes istio的快速安装和使用例子

    安装 [root@master ~]# wget https://github.com/istio/istio/releases/download/1.1.5/istio-1.1.5-linux.ta ...

  5. VS2010-MFC(MFC常用类:CFile文件操作类)

    转自:http://www.jizhuomi.com/software/234.html CFile类概述 如果你学过C语言,应该知道文件操作使用的是文件指针,通过文件指针实现对它指向的文件的各种操作 ...

  6. HDU-2095-find your present (2)-位或/STL(set)

    In the new year party, everybody will get a "special present".Now it's your turn to get yo ...

  7. PAT甲级——A1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...

  8. 关于jar包启动遇到的问题

    一.找不到propertites文件,错误如下 原因是打成的jar不包含classpath信息,需要运行时指定,命令为 -Xbootclasspath/a: 后缀在核心class搜索路径后面.常用! ...

  9. godaddy账号以及域名被盗找回经历以及网络信息安全的思考

    本案涉及到公司的一些机密信息,因此涉及到机密信息,我都将会用一些其他的代号进行替代.不影响读者理解本案.我会按照时间顺序讲述本案经过,是如何一步步找回godaddy账号的. 我供职的公司是一家网络科技 ...

  10. 使用了@Slf4j log没有info的方法 .info()方法爆红或者log爆红

    在springboot项目中,使用注解@Slf4j时,log变量不能用. 导包用的是 import lombok.extern.slf4j.Slf4j; <dependency> < ...