使用Vuex实现一个笔记应用的方法(4)
1. Vue.use(Vuex) 是什么意思?
使用 Vuex,今后用 Vue-router 时也得来这么一出,只是得写在 route 文件夹下的 index.js 文件中
2. +new Date() 是什么意思?
获取时间戳的另一种写法,等同于 new Date().getTime()
3.state,getters,mutations,actions 之间的关系?
state:如上所言状态仓库
getters:state 的修饰,比如 state 中有 str:"abc" 这么个属性,而在很多组件中需要进行 str + "def" 的操作,如果在每个组件都进行 str + "def" 的操作未免太麻烦,于是可以在 getters 中增加:
strAdd(){
return this.str + "abc"
}
今后在组件中使用 strAdd 就可以了
- mutations:简单讲就是用来修改 state 的,同步方法.常规调用 this.$store.commit
- actions:简单讲用来调用 mutations 的,异步方法.常规调用 this.$store.dispatch
四、tool.vue
<template>
<div id="tool">
<button class="add" @click="add_note">新增</button>
<button class="fav" @click="fav_note">收藏</button>
<button class="del" @click="del_note">删除</button>
</div>
</template>
<script type="text/javascript">
import { mapState, mapGetter, mapActions } from 'vuex'
export default {
name: 'tool',
methods:{
...mapActions(['add_note','del_note','fav_note'])
}
}
</script>
<style type="text/css" scoped>
#tool {
width: 200px;
height: 600px;
border: 2px solid #ccc;
float: left;
}
button {
width: 100%;
height: calc(100% / 3);
font-size: 60px;
}
</style>
1.mapState, mapGetter, mapActions 都是什么?
这里有个非常好的解释 http://www.imooc.com/article/14741
此外,当 methods 和 Vuex 的 actions 中具有同名的属性 A 时,可使用 mapActions(['A']) 这种方式简写
注意:1、中括号不能省略;2、中括号内是字符串;3、展开运算符...不能省略
也可以取个别名,写法如下,注意 [] 变成了 {}:
...map({
本组件的属性 : Vuex 中 actions 中的属性
})
需要传入参数时,前提是 actions 中的属性(方法)能接收参数:
methods:{
...mapActions(['abc'])
// 自定义一个方法,通过触发这个方法调用之前重名的方法并传入参数
tragger_abc(参数){
this.abc(参数)
}
}
2.scoped
对当前组件生效的 CSS
3.calc
使用时记得在运算符前后各加一个空格
五、list.vue

