因为之前参照微信的原生的文档写过一些小程序的demo,写的过程比较繁琐,后来出了美团的mpvue,可以直接使用vue开发,其他的不作对比,这篇文章记录一下踩坑之旅.

参照mpvue http://mpvue.com/mpvue/#_2的官网的五分钟的教程搭建项目

在创建的过程中,需要输入微信小程序的个人appId,可以去微信小程序开发平台复制进去。

创建完成后的项目其实和使用vue-cli创建的脚手架大同小异,没图案也只是对其封装,然后转化为wxml。

1.首先引进iview weapp的组件库https://weapp.iviewui.com/docs/guide/start

官网是将需要使用的组件,按照小程序的自定义封装组件的方式,将所需要的组件封装成小程序的自定义的组件,然后通过usingComponents的方式将你需要的组件引进来。

下面是一些例子:

pages/logs/index.vue(此处是文件的路径):

 <template>
<div class="followRecords">
<i-button @click="handleClick">默认按钮</i-button>
<i-button @click="handleClick" type="error" long="true">联通两边按钮</i-button>
<i-button @click="handleClick" type="primary">Primary</i-button>
<i-button @click="handleClick" type="ghost">Ghost</i-button>
<i-button @click="handleClick" type="info">Info</i-button>
<i-button @click="handleClick" type="success">Success</i-button>
<i-button @click="handleClick" type="warning">Warning</i-button>
<i-button @click="handleClick" type="error">Error</i-button>
</div>
</template>
<script>
export default {
name: 'followRecords',
data () {
return {
}
},
mounted () {
},
methods: {
handleClick(e) {
console.log(e);
}
}
}
</script>
<style lang="scss">
.followRecords {
}
</style>

pages/logs/main.json

{
"navigationBarTitleText": "首页",
"navigationBarBackgroundColor": "#000",
"usingComponents": {
"i-card": "../../../static/iview/card/index",
"i-steps": "../../../static/iview/steps/index",
"i-step": "../../../static/iview/step/index",
"i-button": "../../../static/iview/button/index",
"i-divider": "../../../static/iview/divider/index",
"i-panel": "../../../static/iview/panel/index",
"i-toast": "../../../static/iview/toast/index",
"i-message": "../../../static/iview/message/index",
"i-icon": "../../../static/iview/icon/index",
"i-cell-group": "../../../static/iview/cell-group/index",
"i-cell": "../../../static/iview/cell/index",
"i-grid": "../../../static/iview/grid/index",
"i-grid-item": "../../../static/iview/grid-item/index",
"i-grid-icon": "../../../static/iview/grid-icon/index",
"i-grid-label": "../../../static/iview/grid-label/index",
"i-row": "../../../static/iview/row/index",
"i-col": "../../../static/iview/col/index",
"i-tag": "../../../static/iview/tag/index",
"ec-canvas": "../../../static/ec-canvas/ec-canvas"
}
}

此处做一个提示,mpvue创建的pages下的目录需要自行创建一个main.json文件,对该页进行配置,效果如下,颜色还是挺好看的:

这就是将iview的组件引入使用的过程。

这里提出一些遇到的问题,在app.json配置了tabBar的时候,它不一定会显示出来,解决办法:关闭微信开发工具重新打开,或者重新npm run dev一遍吧,记得在某处看过mpvue的解释,他们觉得重新添加的页面也不会太多,所以重启一下也没事大的影响。

在使用的过程中tabBar的icon会404,还是重启微信开发工具,理由就不解释了

在mpvue里使用echarts怎么办,其实echarts也是封装了小程序的组件 https://github.com/ecomfe/echarts-for-weixin,将下载下来的ec-canvas放到微信的目录里,从上面的截图里看一看到,我是将他的dist目录放到了static,引用的路径是'../../../static/iview/..'

有的时候放到src/assets/的时候会一直报错找不到文件,是因为dist/assets没有找到,你需要把他复制下来放到assets/,应该是打包没有打包下来

引用ec-canvas的方法和iview一样,在usingComponents里加入

"ec-canvas": "../../../static/ec-canvas/ec-canvas",参照你个人的路径
在index.vue里使用的时候,只需要传入ec即可
代码如下
<template>
<div class='container'>
<div class="pie-content">
<div class="diseease-info">
<div class="title">疾病分类情况</div>
<div class="select">
<picker class="weui-btn" @change="PickerChange" :value="indexPicker" :range="array">
<view class="picker">
筛选:<span>{{selectTime}}</span>
</view>
</picker>
</div>
</div>
<div class="disease-echart">
<ec-canvas class="canvas" id="mychart-dom-bar" canvas-id="mychart-bar" :ec="ec"></ec-canvas>
</div>
</div>
</div>
</template> <script>
// 此处 opyions为echarts的setOptions
const options = {
backgroundColor: "#fff",
color: ["#f36837", "#5e65e3", "#5e98e3", "#59d0bd", "#f9df94", "#0f0"],
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b}: {c} ({d}%)"
},
legend: {
orient: 'vertical',
left: '65%',
y: 'middle',
data: ['北京', '武汉', '杭州', '广州', '上海'],
textStyle: {
color: "#666",
fontWeight: 'normal'
}
},
grid: {
top: '0%',
left: '0%',
containLabel: true,
},
series: [{
label: {
show: false
},
type: 'pie',
center: ['30%', '50%'],
radius: ['40%', '55%'],
data: [
{
value: 55,
name: '北京'
}, {
value: 20,
name: '武汉'
}, {
value: 10,
name: '杭州'
}, {
value: 20,
name: '广州'
}, {
value: 38,
name: '上海'
}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 2, 2, 0.3)'
}
}
}]
};
export default {
data () {
return {
indexPicker: 3,
array: ['全部', '近七天', '近三个月', '近半年', '近一年'],
selectTime: '',
userInfo: {}, // 用户信息
ec: {
options: options
},
ecLine: {
options: lineOptions
}
}
},
mounted () {
this.checkTime() },
onShow () {
},
watch: {
// ..
},
created () {
// 调用应用实例的方法获取全局数据
this.getUserInfo()
},
methods: {// 选择时间
PickerChange(e) {
this.selectTime = this.array[e.mp.detail.value]
this.array.forEach( (item, index) => {
if(this.array[e.mp.detail.value] === item) {
this.indexPicker = index
}
})
},
checkTime() {
this.array.forEach( (item, index) => {
if(this.indexPicker === index) {
this.selectTime = this.array[index]
}
})
}
}
}
</script> <style lang="scss" scoped>
// 标题title
@mixin diseeaseInfo {
height: 78rpx;
line-height: 78rpx;
color: #666;
font-weight: bold;
.title {
float: left;
text-indent: 20rpx;
font-size: 30rpx;
}
.select {
float: right;
font-size: 28rpx;
padding-right: 2em;
span {
font-weight: normal;
}
}
}
// 宽高
@mixin size($width, $height, $background) {
width: $width;
height: $height;
background: $background;
}
.header {
margin-bottom: 10rpx;
padding: 30rpx 24rpx;
width: 706rpx;
height: 406rpx;
background: #f8f8f8;
.header-info {
height: 162rpx;
margin: 10rpx 0 42rpx;
img {
width: 162rpx;
height: 162rpx;
float: left;
border-radius: 50%;
}
p{
line-height: 46rpx;
width: 420rpx;
font-size: 30rpx;
color: #818181;
padding-left: 33rpx;
float: left;
text-indent:2em;
}
.search {
float:right;
width:30rpx;
margin-right:34rpx;
}
}
.header-message { padding: 28rpx 20rpx;
width: 666rpx;
background: #fff;
box-shadow:0 0 10rpx rgba(0, 0, 0, 0.25);
display:flex;
border-radius:10rpx;
.message {
flex: 1;
height: 120rpx;
border-left: 1rpx solid #e5e5e5;
color: #6c6c6c;
text-align: center;
p {
font-size: 26rpx;
line-height:50rpx;
.number {
font-size:38rpx;
color:#333;
}
.name {
color:#f36800;
}
}
}
.message:nth-of-type(1) {
border: none;
}
}
}
.pie-content {
margin-bottom: 20rpx;
@include size(100%, 380rpx, #fff);
.diseease-info {
@include diseeaseInfo;
}
.disease-echart, .medicine-echart {
@include size(100%, 300rpx, #fff);
.canvas {
@include size(100%, 300rpx, #fff);
}
}
}
.line-content {
margin-bottom: 20rpx;
@include size(100%, 510rpx, #fff);
.diseease-info {
@include diseeaseInfo;
}
.medicine-echart {
@include size(92%, 400rpx, #fff);
margin: 2% 4%;
border-radius: 3%;
overflow: hidden;
.canvas {
@include size(100%, 100%, #fff);
}
}
}
.care-content {
margin-bottom: 20rpx;
@include size(100%, 610rpx, #fff);
.diseease-info {
@include diseeaseInfo;
}
.care-list {
margin: 2% 4%;
@include size(92%, 500rpx, #fff);
overflow-y: auto;
i-row {
font-size: 26rpx;
line-height: 65rpx;
color:#666;
height: 65rpx;
i-col {
border-bottom: 1rpx solid #666;
}
}
} } </style>

效果图如下:

友情提示:微信小程序里是没有select这个标签的,微信的组件里封装了picker组件,其实是对select的一种另类展示,因为微信小程序是针对移动端的,点点点不容易点到select里的其他选项,所以没有select吧,我瞎猜的,。。。

在这里也提一下mpvue的缺点,mpvue并没有将微信小程序里的数据双向绑定实现,再多出api中均存在这样的问题,虽然不影响使用,也就是多写几行代码,以下举出几个例子,

在使用i-tab的时候,value其实是没有根据变化而变化的,当你点击的时候,你需要手动改变iview的value,然后他才会跳到点击的tab,其他的组件都有这种问题,大家慢慢体会吧。。。

微信小程序之mpvue+iview踩坑之旅的更多相关文章

  1. 微信小程序性能测试之jmeter踩坑秘籍(前言)

    最近要做个微信小程序的性能压测,虽然之前只做过web端的,但想一想都是压后端的接口,所以果断答应了下来,之前对jmeter都是小打小闹,所以趁着这次机会好好摆弄摆弄. ---------------- ...

  2. 微信小程序之蓝牙 BLE 踩坑记录

    前言 前段时间接手了一个微信小程序的开发,主要使用了小程序在今年 3 月开放的蓝牙 API ,此过程踩坑无数,特此记录一下跳坑过程.顺便开了另一个相关的小项目,欢迎 start 和 fork: BLE ...

  3. 总结微信小程序开发中遇到的坑

    总结微信小程序开发中遇到的坑,一些坑你得一个一个的跳啊,/(ㄒoㄒ)/~~ 1,页面跳转和参数传递实例 首先说一下我遇到的需求有一个我的消息页面,里面的数据都是后端返回的,返回的数据大致如下,有一个是 ...

  4. 微信小程序搭建mpvue+vant+flyio

    导语 上一篇文章微信小程序搭建mpvue+vant已经介绍了如何搭起mpvue项目及引入vant,本篇文章继续在它的基础上,引入flyio,并做一些封装,目的是为了在小程序发起请求. 这时读者会有些疑 ...

  5. 微信小程序搭建mpvue+vant

    第一步:查看是否已经装了node.js $ node -v $ npm -v   正确姿势 没有装的话前往Node.js官网安装 第二步:安装cnpm $ npm install -g cnpm -- ...

  6. 使用Vue开发微信小程序:mpvue框架

    使用Vue开发微信小程序:mpvue框架:https://www.jianshu.com/p/8f779950bfd9

  7. 微信小程序开发技巧及填坑记录

    以下是自己在开发过程中遇到的坑和小技巧,记录以下: 1.出现了 page[pages/XXX/XXX] not found.May be caused by :1. Forgot to add pag ...

  8. 小程序语音红包开发中 汉字转拼音的问题 微信小程序红包开发遇到的坑

    公司最近在开发微信小程序的红包功能,语音红包需要用到文字转拼音的功能. 之前介绍过怎么将中文的汉字转为拼音的,具体看下面这篇文章. 微信语音红包小程序开发如何提高精准度 红包小程序语音识别精准度 微信 ...

  9. 微信小程序那些令人眼泪汪汪的坑儿

    前言 最近做了一个麻雀虽小,五脏俱全的微信小程序项目.一看就会,一用就废的小程序.有些坑真的坑的你两眼泪汪汪.我就爱干前人栽树后人乘凉的事儿,看到文章的你,也许是同道中人,相视一笑:亦或是小程序外围人 ...

随机推荐

  1. [原]unity5 AssetBundle 加载

    本文unity版本5.1.3 一.现有的打包教程: 1.http://liweizhaolili.blog.163.com/blog/static/16230744201541410275298/ 阿 ...

  2. spring事务解析

    1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是 ...

  3. Golang 笔记 1 基础、基本数据类型

    一.Go语言基础 1. 基础 Go语言中的标识符必须以字母(Unicode字母,PHP/JS可以用中文作为变量名)下划线开头.大写字母跟小写字母是不同的:Hello和hello是两个不同的名字.  G ...

  4. Qt编写自定义控件7-自定义可拖动多边形

    前言 自定义可拖动多边形控件,原创作者是赵彦博(QQ:408815041 zyb920@hotmail.com),创作之初主要是为了能够在视频区域内用户自定义可拖动的多个区域,即可用来作为警戒区域,也 ...

  5. Go学习笔记(四)Go自动化测试框架

    上篇Go学习笔记(三)Go语言学习 Go自动化测试非常简单,在结合VSCode的,让测试完全自动化 一 .编辑器下测试 1.测试代码以xxx_test.go方式命名 2.测试函数要以 func Tes ...

  6. jenkins git 之 Advanced clone behaviours

    jenins 上的 Git Plugin插件,默认是下载完整的历史版本,随着分支约多,历史版本约多,整个文件会很大,下载常常会超时. 单独的git命令可以使用以下方式来优化 git clone --d ...

  7. ajax-addclass

  8. linux的基本操作(RPM包或者安装源码包)

    RPM包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下.所以你要 ...

  9. 协程greenlet、gevent

    greenlet为了更好使用协程来完成多任务,python中greenlet模块对其封装,从而使得切换任务变得更加简单安装方式 pip3 install greenlet 示例代码: from gre ...

  10. mysql数据库数据的 备份以及还原

    数据库备份的3种方式: 例如:mysqldump -uzx_root -p test>/root/test1.sql