import Vue from 'vue'
// 这里引入vuex
import Vuex from 'vuex' Vue.use(Vuex)
export default new Vuex.Store({
// state里面的是全局的属性
state:{
num:0,
price:15,
name:'葡萄',
testList:[]
},
// mutations里面的是全局的方法 参数state是固定写法 可以获取参数
// 用这样的方式去调用 this.$store.commit('xxx');
mutations:{
add(state){
state.num++ ;
console.log(state.num)
}
},
// getters是Vuex里的属性计算参数state是固定写法 可以获取参数
// 调用方法 this.$store.getters.count
// Vuex的计算属性,在视图中当变量使用
// 计算属性依赖一个可变的属性 只要这个属性发生变化 这个函数就会自动执行
getters:{
count(state){
// 返回一个计算好的值
return state.num*state.price
}
},
// 异步方法 用这样的方式去调用 this.$store.dispatch('xxx');
actions:{
testActions(context){
// context里面包含了state mutations getters actions的方法及属性可以直接调用
// 执行一些异步的操作或者通用的ajax请求
setTimeout(()=>{
context.state.testList = ['大娃','二娃','三娃','四娃','五娃']
},2000)
}
}
})

html

<template>
<view>
<view>{{ datas }}</view>
<view>数量:{{ num }}</view>
<view>{{ name }}</view>
<view>总价:{{count}}</view>
<button type="primary" @click="add">add</button>
<button type="primary" @click="testActions">testActions</button>
<view>
<view v-for="(item,index) in testList" :key='index'>
{{item}}
</view>
</view>
<!-- <view>
<uni-calendar
:insert="true"
:lunar="true"
:disable-before="true"
:start-date="'2019-3-2'"
:end-date="'2019-5-20'"
@change="change"
></uni-calendar>
</view> -->
</view>
</template> <script>
// 把下载好的组件引进要使用的地方
import uniCalendar from '../../components/uni-calendar/uni-calendar.vue'; export default {
data() {
return {
datas: '',
// 可以值获取到name的值
name:this.$store.state.name
};
},
// 记得要在components里面去局部注册
components: {
uniCalendar
},
onReady() {
this.getajax();
},
computed: {
// 需要在计算属性里面设置
num() {
return this.$store.state.num;
},
count(){
return this.$store.getters.count;
},
testList(){
return this.$store.state.testList;
}
},
methods: {
getajax() {
uni.request({
url: 'https://bird.ioliu.cn/weather', //仅为示例,并非真实接口地址。
data: {
city: '北京'
},
header: {
'custom-header': 'hello' //自定义请求头信息
},
success: res => {
console.log(res.data);
this.datas = res.data.basic.city;
console.log(this.datas);
}
});
},
add() {
// 这里用this.$store.commit(xxx')去调用方法
this.$store.commit('add');
},
testActions(){
this.$store.dispatch('testActions');
}
}
};
</script> <style lang="scss">
uni-rate {
height: 200px;
}
</style>

最新文章

  1. easyUI + swfupload 多附件上传功能
  2. 关于sql用&lt;&gt;不等于查询数据不对问题
  3. css之让文字在一定范围内显示,不超过固定的宽度和高度
  4. jsp应用bootstrap表格应用实例
  5. 导出csv文件
  6. winform 播放声音方式 分类: WinForm 2014-07-25 14:16 194人阅读 评论(0) 收藏
  7. Zend Cache的学习和实例
  8. DFS-hdu-2821-Pusher
  9. 第6次结对作业--郑锦伟&amp;古维城
  10. clapack在android上移植
  11. 抓取出现时间3s后消失的元素
  12. SQLServer------遍历操作,游标的基础使用
  13. dbutis事务管理
  14. C#类的修饰符
  15. Druid学习之路 (四)Druid的数据采集格式
  16. SIGBUS 和 SIGSEGV
  17. 使用ZooKeeper实现Java跨JVM的分布式锁
  18. ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库
  19. Centos 常用系统命令
  20. 关于thinkphp5中数据库分组查询group

热门文章

  1. 组合数取模(lucas定理+CRT合并)(AC)
  2. POJ1190 洛谷P1731 NOI1999 生日蛋糕
  3. TyvjP2018 「Nescaf&#233;26」小猫爬山
  4. php_imagick是怎么实现复古效果的呢?
  5. Windows10 +Ubuntu 18.04双系统安装详细教程
  6. Effective Modern C++:07并发API
  7. 【机器学习PAI实战】—— 玩转人工智能之商品价格预测
  8. 【JZOJ4814】【NOIP2016提高A组五校联考2】tree
  9. SDUT-3364_欧拉回路
  10. JavaScript void