用mpvue写个玩意儿玩玩
下周公司要搞黑客马拉松了,组里可能会做个小程序。然后看到了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写个玩意儿玩玩的更多相关文章
- Delphi - 闲来无事,自己写个Timer玩玩(多线程Timer)
明天去坐火车,回家,今天就没有事做,本来在弄一个跨进程获取其他程序里面组件,如ListView,ListBox,Button等的信息,突然有个想法自己写个Timer,不用SetTimer函数,我们自己 ...
- 手贱,写个call玩玩.
今天在睡觉醒时,突然感觉对call和apply有了点理解,但是又不好表达出来.就随便写几个例子. function say() { return this.role; } function Fathe ...
- 写个sleep玩玩
static void sig_when_weakup(int no){ printf("weakup weakup\n"); longjmp(buf, ); } void wea ...
- 百度翻译api初使用(很久没写python了,写几行玩玩)
调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...
- 让你从零开始学会写爬虫的5个教程(Python)
写爬虫总是非常吸引IT学习者,毕竟光听起来就很酷炫极客,我也知道很多人学完基础知识之后,第一个项目开发就是自己写一个爬虫玩玩. 其实懂了之后,写个爬虫脚本是很简单的,但是对于新手来说却并不是那么容易. ...
- 【原创】自己动手写工具----XSmartNote [Beta 2.0]
一.前面的话 在上一篇自己动手写工具----XSmartNote中,我简单介绍了这个小玩意儿的大致界面和要实现的功能,看了一下园子里的评论,评价褒贬不一,有人说“现在那么多云笔记的工具”,“极简版ev ...
- javaweb写的在线聊天应用
写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了: 登录界面: 聊天界面: 左侧是在线的用户 ...
- 简单的玩玩etimer <contiki学习笔记之九>
好吧,我承认etimer有点小复杂,主要是它似乎和contiki的process搅在一起,到处都在call_process.那就先搜搜contiki下的etimer的example看看,然后再试着写一 ...
- 如何把原生小程序项目合并的mpvue项目中
当时的情景是这样的: 使用mpvue写微信小程序,写着写着项目写到一半了,突然间不想这样继续写了,想切换回原生小程序语法去写剩余部分. 如下图,红色框里的功能是已经用mpvue完成的功能,绿色框部分的 ...
随机推荐
- Spring Boot 2.x 整合 Redis最佳实践
一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...
- 金山wps的面试经历
故事从两个月前开始说起吧. 前段时间突然想跳槽,原因也没啥,就是想折腾下,看看外面的世界?有一部分原因是想离家近一些稳定下来,博主上份工作坐标厦门,风景好的简直随便拍照就是大片. 不废话了,机缘巧合, ...
- linux上安装 mysql
一.linux 上安装 mysql 1.查看mysql是否安装 rpm -qa|grep mysql 2.卸载 mysql yum remove mysql mysql-server mysql-li ...
- tomcat打印接口延迟时间
项目中有些页面时延不稳定,需要看每次接口调用时延,怎么看,有两种方法:一种是直接去catalina.out日志中看,一种是直接去localhost_access_log日志中看,第一种需要在代码中实现 ...
- html5页面编码如何确定
页面乱码问题建站学之前曾经多次发教程说明,对于新的html5来说我们的编码要如何做才能解决乱码问题呢?作为一个前端工程师,你是如何指定一个页面的编码的呢?你知道浏览器是怎么识别编码的吗? 首先,一个很 ...
- Java设计模-过滤器模式
过滤器模式 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接 ...
- jquery框架概览(二)
(function(window, undefined) { })(window) window对象作为参数传进闭包的好处 JavaScript 全局对象.函数以及变量均自动成为 window 对象的 ...
- Spring+Mybais整合
简单的来说,Spring主要用于在业务层(当然spring也有数据库交互的模块,个人觉得spring在这方面有一点不如mybatis),而mybatis主要用于数据持久化,在一个完整的项目中无论是业务 ...
- 德国、日本的制造业为什么不能完全执行SOP?
在过去几十年,德国.日本的制造企业简直就是"以质取胜"的代名词,一些制造业的CEO非常自豪,甚至在公开场合调侃:大家好,我就是"保质保量"本人,也正因如此,德国 ...
- gin源码剖析
介绍 Gin 是一个 Golang 写的 web 框架,具有高性能的优点,基于 httprouter,它提供了类似martini但更好性能(路由性能约快40倍)的API服务.官方地址:https:// ...