下周公司要搞黑客马拉松了,组里可能会做个小程序。然后看到了mpvue感觉还不错,于是就打算试试水。用vue写小程序听上去美滋滋。
那么先开始吧!

全局安装 vue-cli

$ npm install --global vue-cli

创建一个基于 mpvue-quickstart 模板的新项目

$ vue init mpvue/mpvue-quickstart my-project

安装依赖

$ cd my-project
$ npm install

启动构建

$ npm run dev

这样子就Okay了。跑起来之后,在微信开发工具里新建项目,选择my-project下的dist目录

然后确定,你就能看到你的小程序已经可以运行了。项目请用别的编辑去编辑,vscode和atom都可以。微信开发工具仅用于调试。

我在pages下面新建了一个todolist和weather页面。每个目录下都有一个.vue文件和一个main.js文件。main.js下面可以配置一个微信小程序的参数,vue文件就是我们要编辑的页面了。

在打开src/main.js文件,在pages字段上加上我们刚刚创建的两个页面的路径。

接下来在src/components下创建一个组件我叫他todo-list.vue
代码如下,自己瞎几把写写的,各种div和css请不要在意,名字也取得不好。

src/components/todo-list.vue
<template>
<div class='container'>
<h3>{{say}}</h3>
<div>
<div class='userinfo'>
<input type="text" v-model='value' placeholder="请输入" class='input'>
<div @click='handleClick' class='button'>Add</div>
</div>
<ul>
<li v-for='(item, index) in items' v-bind:key='index'>
<span @click='handleToggle(index)' v-bind:class='{done: item.completed}' class='item'>{{index + 1}}、{{item.name}}</span>
<div @click.prevent='handleRemove(index)' class='button'>remove</div>
</li>
</ul>
</div>
</div>
</template> <script>
export default {
data () {
return {
value: '',
}
},
props: ['say', 'items'],
methods: {
handleClick() {
if (this.value) {
this.$emit('addTodo', this.value)
this.value = ''
}
},
handleToggle(index) {
this.$emit('toggleItem', index)
},
handleRemove(index) {
this.$emit('removeItem', index)
}
}
}
</script> <style scoped>
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}
.done {
color: red;
text-decoration: line-through;
}
.item {
font-size: 40rpx;
line-height: 100rpx;
display: inline-block;
height: 100rpx;
width: 550rpx;
}
.button {
width: 160rpx;
display: inline-block;
height: 70rpx;
font-size: 40rpx;
background: #ccc;
border-radius: 20rpx;
text-align: center;
}
.input {
display: inline-block;
padding: 0 12px;
margin-bottom: 5px;
border: 1px solid #ccc;
}
</style>

写完了组件,再写src/pages/todolist/index.vue

src/pages/todolist/index.vue
<template>
<div>
<todolist v-on:addTodo='saveValue' v-on:toggleItem='toggleItem' v-on:removeItem='removeItem' v-bind='motto'></todolist>
</div>
</template> <script>
import todolist from '@/components/todo-list.vue' export default {
data () {
return {
motto: {
say: 'Hello',
items: wx.getStorageSync('items') || [],
},
}
},
components: {
todolist,
},
methods: {
saveValue(val) {
this.motto.items.push({
name: val,
completed: false,
})
wx.setStorageSync('items', this.motto.items)
},
toggleItem(index) {
this.motto.items[index].completed = !this.motto.items[index].completed
wx.setStorageSync('items', this.motto.items)
},
removeItem(index) {
this.motto.items.splice(index, 1)
wx.setStorageSync('items', this.motto.items)
}
}
}
</script> <style scoped> </style>

这里我用wx.getStorageSync存储了todolist的数据。

接下来我们再写一个weather组件和weather页面吧,名字被我取的一样,罪过。

src/components/weather.vue
<template>
<div>
My Weather~
<div>{{weather.location.path}}</div>
<div>{{weather.now.text}}-{{weather.now.temperature}}摄氏度</div>
<div>穿衣:{{suggestion.dressing.brief}}</div>
</div>
</template> <script>
export default {
data () {
return {
weather: {
location: {
},
now: {},
last_update: '',
},
suggestion: {
dressing: {}
},
}
},
methods: {
setWeather(data) {
this.weather = data
},
setSuggestion(data) {
this.suggestion = data
}
},
mounted() {
var self = this
wx.getLocation({
success(data) {
console.log('location', data)
let {latitude, longitude} = data;
let location = `${latitude}:${longitude}`
wx.request({
url: `https://api.seniverse.com/v3/weather/now.json?key=123456789&location=${location}&language=zh-Hans&unit=c`,
success(res) {
console.log('weather', res)
let {location, now, last_update} = res.data.results[0]
self.setWeather({location, now, last_update})
}
})
wx.request({
url: `https://api.seniverse.com/v3/life/suggestion.json?key=123456789&location=${location}&language=zh-Hans`,
success(res) {
console.log('生活指数', res)
let {suggestion} = res.data.results[0]
self.setSuggestion(suggestion)
}
})
}
})
}
}
</script> <style scoped> </style>
src/pages/weather/index.vue
<template>
<div>
<weather></weather>
</div>
</template> <script>
import weather from '../../components/weather' export default {
data () {
return { }
},
components: {
weather,
},
methods: { }
}
</script> <style scoped> </style>

这里用到的接口

https://api.seniverse.com/v3/...${location}&language=zh-Hans&unit=c

大家去www.seniverse.com自己注册一个就可以了。
这里我们现在用wx.getLocation获取地理位置,我们会用到经纬度。然后再wx.request()去调接口获取天气数据。
你以为这样就完了,事情不是这样的。我们还要在小程序官网上填写服务器域名。如下图

最后我们可以把这两个page用起来了

我们在src/pages/index/index.vue下加上两个按钮

<template>
<button @click='onTodo'>Todo</button>
<button @click='onWeather'>Weather</button>
</template>

methods里写上页面跳转的方法。

<scirpt>
export default {
methods: {
onTodo() {
const url = '../todolist/main'
wx.navigateTo({url})
},
onWeather() {
const url = '../weather/main'
wx.navigateTo({url})
},
}
}
</script>

到此结束。原谅我不会写flex布局,不会写小程序,样子惨不忍睹?。
补充一下,调用wx.getLocation()之后获取了经纬度之后,还可以玩玩微信的map组件。试着用微信map写一个vue组件。

<map name="location" v-bind:latitude='location.latitude' v-bind:longitude='location.longitude'></map>

另外还可以玩玩微信的camera和canvas组件。
以下是一些小细节
新增的页面不会添加进webpack的 entry,需要重新 npm run dev。
总体上来说用mpvue写小程序,可玩性还是挺高的。vue我也是这两天刚刚现学现卖的,还不大会写。
学完vue之后,在不了解小程序的情况下,你看就可以写出点玩意儿来了。还是挺不错的吧。大大降低了学小程序那一套东西的成本。

用mpvue写个玩意儿玩玩的更多相关文章

  1. Delphi - 闲来无事,自己写个Timer玩玩(多线程Timer)

    明天去坐火车,回家,今天就没有事做,本来在弄一个跨进程获取其他程序里面组件,如ListView,ListBox,Button等的信息,突然有个想法自己写个Timer,不用SetTimer函数,我们自己 ...

  2. 手贱,写个call玩玩.

    今天在睡觉醒时,突然感觉对call和apply有了点理解,但是又不好表达出来.就随便写几个例子. function say() { return this.role; } function Fathe ...

  3. 写个sleep玩玩

    static void sig_when_weakup(int no){ printf("weakup weakup\n"); longjmp(buf, ); } void wea ...

  4. 百度翻译api初使用(很久没写python了,写几行玩玩)

    调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...

  5. 让你从零开始学会写爬虫的5个教程(Python)

    写爬虫总是非常吸引IT学习者,毕竟光听起来就很酷炫极客,我也知道很多人学完基础知识之后,第一个项目开发就是自己写一个爬虫玩玩. 其实懂了之后,写个爬虫脚本是很简单的,但是对于新手来说却并不是那么容易. ...

  6. 【原创】自己动手写工具----XSmartNote [Beta 2.0]

    一.前面的话 在上一篇自己动手写工具----XSmartNote中,我简单介绍了这个小玩意儿的大致界面和要实现的功能,看了一下园子里的评论,评价褒贬不一,有人说“现在那么多云笔记的工具”,“极简版ev ...

  7. javaweb写的在线聊天应用

    写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了: 登录界面: 聊天界面: 左侧是在线的用户 ...

  8. 简单的玩玩etimer <contiki学习笔记之九>

    好吧,我承认etimer有点小复杂,主要是它似乎和contiki的process搅在一起,到处都在call_process.那就先搜搜contiki下的etimer的example看看,然后再试着写一 ...

  9. 如何把原生小程序项目合并的mpvue项目中

    当时的情景是这样的: 使用mpvue写微信小程序,写着写着项目写到一半了,突然间不想这样继续写了,想切换回原生小程序语法去写剩余部分. 如下图,红色框里的功能是已经用mpvue完成的功能,绿色框部分的 ...

随机推荐

  1. 2019-2020-1 20199324《Linux内核原理与分析》第三周作业

    第二章 操作系统是如何工作的 一.知识点总结 1.计算机的三个法宝 存储程序计算机 函数调用堆栈机制.堆栈:是C语言程序运行时必须使用的记录函数调用路径和参数存储的空间. 中断 2.堆栈相关的寄存器和 ...

  2. 隐马尔可夫随机场HMM

    概率知识点: 0=<P(A)<=1 P(True)=1;P(False)=0 P(A)+P(B)-P(A and B) = P(A or B) P(A|B)=P(A,B)/P(B) =&g ...

  3. 在Python 中怎么表示一个元素在一个list中的数量?

    commonest = [1,2,2,2,1,3,4,5,1,1] print(commonest.count(1))

  4. POJ-1811-Prime Test(pollard_rho模板,快速找最小素因子)

    题目传送门 sol:Pollard_Rho的模板题,刚看了Pollard_Rho和Miller_Rabin很多原理性的东西看不懂,只是记住了结论勉强能敲代码. Pollard_Rho #include ...

  5. 吴裕雄--天生自然Android开发学习:android 背景相关与系统架构分析

    1.Android背景与当前的状况 Android系统是由Andy Rubin创建的,后来被Google收购了:最早的版本是:Android 1.1版本 而现在最新的版本是今年5.28,Google ...

  6. CSA|EI

    信息检索 CSA是学科特色的包含相关学科的内容,其网址是https://search.proquest.com/ 可以使用命令行检索: 分类的限制检索: 寻找检索线索可使用百科全书 EI是工程领域最全 ...

  7. keep pace with |sixes and sevens.|Three dozen of |setting out|in despite of|appetite for|brought up|.turn to|leave behind|As can be seen|every

    Heavy but not excessive: network capacity seems to have done little more than keep pace with economi ...

  8. linux基本指令梳理

  9. overflow属性的应用

    在使用JQueryUI chosen插件的时候,由于页面布局的原因,下拉列表框超出div范围,图形效果严重变形,一点解决的思路都没有,最后请教公司前端,瞬间解决,原来使用CSS 中的overflow属 ...

  10. java 使用poi导入Excel通用方法

    需要的jar: [XML] 纯文本查看 复制代码 ? 1 2 3 4 5 <dependency>             <groupId>org.apache.poi< ...