使用Vuex心得
之前一直都是看别人写的vuex感觉还挺好理解的,今天自己根据需求写了下vuex,一下子不知道怎么写了,
想要用好vuex还是先要知道原理:
参考好博客写的非常到位:https://www.cnblogs.com/DM428/p/7293867.html
基本组成:
注意到这个store对象包含三个子对象:
state、mutations、actions
其中state用于存储数据,类似vue实例的data属性。
mutations用于递交更改,对state对象中的属性数据进行更改。
actions用于进行递交异步更改,通过调用mutations实现对数据的更改。
actions与mutations的区别:
其中actions区别于mutations的地方在于mutations只能进行同步更改,而actions中的更改可以是异步执行。所以基本上所有用户执行的直接数据更改都是触发mutations属性
函数执行,而需要与后端进行数据交互的数据更改通常是通过actions属性函数去执行。
定义actions与mutations属性函数的注意事项:
其中定义mutations属性函数时必须传递的第一个参数是state,因为要对state进行更改,第二个参数代表传入的新参数。mutations属性函数只接受两个参数,如果要同时更改多个属性值,可以通过对象传入。
在actions属性函数中可以通过context.commit()方法触发mutations属性函数。定义actions属性函数时,必须传递的第一个参数是context,用于触发mutations函数。
触发actions与mutations属性函数的方法:
在子组件中通过this.$store.commit()方法触发mutations属性函数。在注册store的Vue实例中(第三步中将会讲到)可以通过store.commit()触发。
commit函数第一个参数是mutations的属性函数名,第二个参数是传入的新值。
actions属性函数中可以进行异步操作,比如通过ajax或者Vue.Resource()进行数据获取,获取数据后再通过context.commit()触发更改。
触发actions属性函数使用this.$store.dispatch()或者store.dispatch() (在注册store的Vue实例中)函数。dispatch函数传递的一个参数是actions属性函数名称。如果希望在
Vue实例创建完成还未挂载时就从后端获取数据,则可以在created钩子函数中调用actions属性函数。
在组件中访问数据中心state的注意事项:
在Vue实例中可以通过this.$store.state对象获取state中的数据。如果希望在state中的数据发生更改之后,组件会自动更新,则应该使用组件的computed属性定义数据,而
不是通过data属性定义。如果使用data定义组件数据,则state中的数据发生更改之后组件不会发生变化。
练习:vuex+父子组件间通信
项目目录:
页面组件:AppFilm.vue
<template>
<div class="app-film">
<AppFilmNav></AppFilmNav>
<AppFilmBox></AppFilmBox>
</div>
</template>
<script>
import AppFilmNav from '@/components/AppFilmNav'
import AppFilmBox from '@/components/AppFilmBox'
export default {
name: 'app-film',
components:{AppFilmNav,AppFilmBox},
data () {
return {
}
},
computed:{ }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.app-film{
height: 0.5rem;
}
</style>
AppFilmBox:
<template>
<div class="app-film-box">
<AppFilmItem :film="film" v-for="(film,index) in films" :key="index"></AppFilmItem> /// :film 绑定数据传递给子组件
<!--{{ infor }}-->
</div>
</template>
<script>
import axios from 'axios'
import AppFilmItem from './AppFilmItem'
export default {
// props:['infor'],
name: 'app-film-box',
components:{AppFilmItem},
data () {
return {
// films:[]
}
},
// 必须通过computed属性使用state数据!否则state属性中的数据发生更改时不会反映在组件上!
computed: {
films(){
return this.$store.state.film
}
},
methods: {
getfilms(){
let that = this;
this.$store.dispatch('Toggle',this.$store.state.posturl);
}
},
created(){
this.getfilms()
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.app-film-box{
padding:0 15px;
}
</style>
appFilmItem.vue
<template>
<div class="app-film-item">
<img :src="film.poster" alt="">
<div class="info">
<h5>{{film.name}}</h5>
<p class="for">{{film.intro}}</p>
<p class="for1">{{film.cinemaCount}}家影院上映<i>{{film.watchCount}}人购票</i></p>
</div>
<div class="other">
<div class="range">8.5</div>
<i class="fa fa-angle-right"></i>
</div> </div>
</template>
<script>
export default {
name:'app-film-item',
props:['film'], // 从父组件那里接受数据 film
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
body{
background-color:#f9f9f9;
}
.app-film-item{
// padding-left: 20px;
// margin-top: -0.3rem;
display: flex;
align-items: center;
padding-bottom: 28px;
border-bottom: 1px solid #ccc;
padding-top: 25px;
img{
width:0.6rem;
}
.info{
padding-left: 15px;
display: inline-block;
width: 75%;
h5{
font-size: 16px;
line-height: 32px;
color: #000;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.for{
height: 24px;
line-height: 24px;
color: #8e8e8e;
font-size: 14px;
overflow: hidden;
text-overflow: ellipsis;
width: 100%;
display: inline-block;
}
.for1{
line-height: 24px;
color: #8e8e8e;
font-size: 12px;
i{
margin-left: 15px;
}
} }
}
.other{
display: flex;
justify-content: space-between;
margin-right: 0.2rem;
margin-bottom:0.5rem;
.range{
font-size: 16px;
line-height: 32px;
color: #fc7103;
}
i{
padding-left: 5px;
line-height: 29px;
color: #c6c6c6;
}
}
</style>
AppFilmNav.vue
<template>
<div class="app-film-nav">
<div class="now_playing" @touchend.stop.prevent="toggle(0)" :class="{active:active==0}">
正在热映
</div>
<div class="coming_soon" @touchend.stop.prevent="toggle(1)" :class="{active:active==1}">
即将上映
</div>
</div>
</template>
<script>
export default {
name:'app-film-nav',
data () {
return {
active:0
}
},
methods: {
toggle (i) {
this.active = i;
this.$store.state.posturl = this.$store.state.urls[i].url;
this.$store.dispatch('Toggle',this.$store.state.posturl);
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.app-film-nav{
height: 47px;
margin: 0.5rem 15px 5px 15px;
border-bottom: solid #fe6e00 1px;
// padding-left: 15px;
// padding-right:15px;
.now_playing{
float: left;
width: 50%;
height: 100%;
text-align: center;
font-size: 16px;
line-height: 46px;
// color: #6a6a6a;
cursor: pointer;
}
.coming_soon{
float: left;
width: 50%;
height: 100%;
text-align: center;
font-size: 16px;
line-height: 46px;
color: #6a6a6a;
cursor: pointer;
}
.active{
color: #fe6e00;
border-bottom: solid;
}
}
</style>
vuex写在main.js里
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import axios from 'axios' Vue.use(Vuex)
Vue.config.productionTip = false const store = new Vuex.Store({
state:{
urls:[
{title:'正在热映',url:'/static/mock/now-playing.json'},
{title:'即将上映',url:'/static/mock/coming-soon.json'}
],
posturl:'/static/mock/now-playing.json',
film:''
},
mutations:{
SET_FILM: (state, film) => {
state.film = film
}
},
actions: {
Toggle: ({commit},url) => {
return new Promise((resolve, reject) => {
axios.get(url).then((res) => {
const data = res.data.data
commit('SET_FILM', data)
resolve()
}).catch((err) => {
reject(err)
})
})
}
}
}) /* eslint-disable no-new */
new Vue({
el: '#app',
store, // 根组件通过store选项将store实例注入所有地子组件
created (){
store.dispatch('Toggle',store.state.posturl);
},
router,
components: { App },
template: '<App/>'
}) // 上面的代码能够让子组件通过this.$store访问到store实例。
页面效果:
这里用Vuex来切换get请求的地址
使用Vuex心得的更多相关文章
- vue-cli中配置vuex流程和注意事项
本文目录 vue-cli下新建站 配置路由更改HelloWorld.vue组件到新建Home.vue组件 安装vuex 测试是否安装成功vuex一:vue-cli下新建站 a)新建文件夹vuexStu ...
- Vuex 实际使用中的一点心得 —— 一刷新就没了
问题 在开发中,有一些全局数据,比如用户数据,系统数据等.这些数据很多组件中都会使用,我们当然可以每次使用的时候都去请求,但是出于程序员的"洁癖"."抠"等等优 ...
- vuex的使用心得
今天的工作内容-----vuex的使用心得: 都知道,对于小型的项目来说不必使用vuex,但是对于需要把共享的变量全部存储在一个对象里面,然后把这个对象放在顶层组件中以供其他组件使用.其实vuex就是 ...
- vuex使用心得分享(填坑)
今天我们简单说一下vuex的使用,vuex是什么呢,相当于react的redux,如果项目使用数据过多的话,直接管理是非常不方便的,那么采用vuex,那些繁琐的问题就迎刃而解了,首先我们先看看官方对v ...
- Vuex学习心得
最近公司项目中使用Vuex做状态管理,就全面温习了一遍文档,然后在项目使用中遇到一些常见问题就一一总结下. 一.由来 我们知道Vue中数据是自顶向下单向流动的,但是以下两种情况单向数据流实现起来十分繁 ...
- 探索 vuex 2.0 以及使用 vuejs 2.0 + vuex 2.0 构建记事本应用
前言 首先说明这并不是一个教程贴,而记事本应用是网上早有的案例,对于学习 vuex 非常有帮助.我的目的是探索 vuex 2.0 ,然后使用 vue 2.0 + vuex 2.0 重写这个应用,其中最 ...
- vue学习心得
前言 使用vue框架有一段时间了,这里总结一下心得,主要为新人提供学习vue一些经验方法和项目中一些解决思路. 文中谨代表个人观点,如有错误,欢迎指正. 环境搭建 假设你已经通读vue官方文档(文档都 ...
- Vuex 2.0 深入简出
最近面试充斥了流行框架Vue的各种问题,其中Vuex的使用就相当有吸引力.下面我就将自己深入简出的心得记录如下: 1.在vue-init webpack project (创建vue项目) 2.src ...
- vuex到底是个啥
vuex总结 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的 ...
随机推荐
- OAuth2.0 授权码理解
OAuth2.0授权模式 本篇文章介绍OAuth的经典授权模式,授权码模式 所谓授权无非就是授权与被授权,被授权方通过请求得到授权方的同意,并赋予某用权力,这个过程就是授权. 那作为授权码 ...
- mysql外键使用
一.外键 .外键:链接两张表的字段,通过主表的主键和从表的外键来描述主外键关系,呈现的是一对多的关系.例如:商品类别(一)对商品(多),主表:商品类别表,从表:商品表. .外键的特点:从表外键的值是对 ...
- 45.work_struct和delayed_work的工作队列使用
介绍 在中断处理中,经常用到工作队列,这样便能缩短中断处理时的时间 中断中通过调用schedule_work(work)来通知内核线程,然后中断结束后,再去继续执行work对应的func函数 示例 当 ...
- (9)Microsoft office Word 2013版本操作入门_文本框_word排版
1.插入文本框 :[插入]---[文本框]可以自己画,也可以选择已有的模板 2.文本框设置 :点中文本框选择[格式]---[对齐文本]可以让文字上下居中,[形状填充]填充不同的颜色.[形状轮廓]可以改 ...
- Spring Boot 2.0 升级指南
Spring Boot 2.0 升级指南 前言 Spring Boot已经发布2.0有5个月多,多了很多新特性,一些坑也慢慢被填上,最近有空,就把项目中Spring Boot 版本做了升级,顺便整理下 ...
- myeclipse无法部署项目的解决
一.问题 myeclipse无法部署项目,点击这个部署按钮没有反应. 二.解决办法 1.找到myeclipse的工作空间,也就是启动时的那个项目保存的空间,我的是在D:\myeclipse_works ...
- CSS table-layout 属性
设置表格布局算法: table { table-layout:fixed; } 所有浏览器都支持 table-layout 属性. 定义 tableLayout 属性用来显示表格单元格.行.列的算法规 ...
- linux服务器重启指令
一.Linux 的五个重启命令 1.shutdown 2.poweroff 3.init 4.reboot 5.halt 二.五个重启命令的具体说明 shutdown reboot 在linux下一些 ...
- margin:0 auto是什么意思
一.margin设置对象外边距 二.margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right 因为0 auto
- Python入门基础之list和tuple
Python之创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: > ...