uni-app学习记录06-Vuex简单使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了uni-app学习记录06-Vuex简单使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2444字,纯文字阅读大概需要4分钟。
内容图文
import Vue from ‘vue‘ // 这里引入vuex import Vuex from ‘vuex‘ Vue.use(Vuex) export defaultnew 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() { returnthis.$store.state.num; }, count(){ returnthis.$store.getters.count; }, testList(){ returnthis.$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>
原文:https://www.cnblogs.com/wanguofeng/p/11747507.html
内容总结
以上是互联网集市为您收集整理的uni-app学习记录06-Vuex简单使用全部内容,希望文章能够帮你解决uni-app学习记录06-Vuex简单使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。