这篇文章主要介绍了使用vuex实现一个笔记应用的方法,现在分享给大家,也给大家做个参考。
最近开始着手学习 vue,先大略的过了一遍官方文档,跟着敲过一部分官方文档中的 demo,然而还是不甚了了。在网上找到了一个入门的笔记应用,即便是入门级的应用,在学习途中依旧困难重重。特将学习作此笔记,方便今后回看,也希望能够帮到刚开始学习 vue 的女同学
预期目标
笔记具备如下基本功能
1.新增
2.删除
3.收藏
4.在全部笔记和收藏笔记间切换
5.在当前列表中进行搜索
卖家秀
买家秀
准备工作
1.新建项目
选个文件夹存放项目,这里我用的是 git bush 执行语句($ 符号是 git bush 中自带的),你也可以使用命令行,一样的
选择项目存放位置
2.查看模块(爱看不看)
查看一下全局安装的模块 npm list --depth=0 -global
查看全局安装的模块
3.创建项目
在命令行输入 vue init webpack vuex-note 并做好设置,创建一个项目
这都什么鬼
4.简单解释一下各个配置都是干嘛的
vue init webpack vuex-note:初始化(init)一个使用 webpack 构建工具构建的 vue 项目,项目名为 vuex-note
project name:项目名
project description:项目描述
author:朕
vue build:构建方式,分为独立构建和运行时构建,具体说明见如下链接,这里选择独立构建 standalone https://vuejs.org/v2/guide/installation.html#runtime-compiler-vs-runtime-only
install vue-router:是否需要安装 vue-router ,跳转页面用的,这里用不着,我过会学
use eslint to lint your code:eslint 规范与法用的,可能你熟悉的写法都是不标准的,如果采用 eslint 则可能报错,这里选择 n
剩下的都是测试用的,一路 n
should we run 'npm install' for you after the project has been created:是否需要直接替你安装(npm install)相关的依赖,回车就行,之后会替你安装各种玩意
5.安装完后会有提示,我们接着按照提示走
先是 cd vuex-note 进入刚刚创建的 vue 项目文件夹
安装完成
再通过 npm run dev 跑起项目
后续操作
6.访问页面
此时通过浏览器访问 localhost:8080 就可以打开一个新的 vue 页面
崭新的 vue 页面
7.项目结构
截止目前的项目结构如图
项目结构
由于是初学,为了先搞个东西出来,所以暂时先不管一些乱七八糟的配置,只挑跟这次相关的说(其实多了我也没学到...)
8.查看 vuex
既然是使用 vuex 来实现笔记应用,我们就应该先查看一下构建的项目是否包含 vuex 模块。
node_modules 文件夹包含了现有的模块,然而里面并没有我们想要的 vuex,不信自己去看
package.json 文件描述了项目包含的文件,项目如何运行等信息
package.json
9.安装 vuex
在命令行中输入 npm install vuex --save:--save 就是将安装信息写入 package.json
已安装了 vuex
至此,所有前期工作已经准备完成,遗漏的部分将在实现过程中逐一解释
搞起
零、思路
整个应用可拆分为三个组件
拆
每条笔记包括 编号(id),标题(title),内容(content),是否已收藏(fav) 四种信息
vuex 中的 state 得有个地方存放 所有的笔记(notes)
而 收藏,删除 操作只能对 当前的笔记 进行操作,因此我还需要一个标识用来记录 当前的笔记(activenote) 是哪个
包含 全部 和 收藏 两种切换方式,因此还需要有一个标识来进行区分,就叫 show 吧,all 代表 全部,fav 就代表 已收藏
组件 ==> actions.js ==> mutations.js = > state:通过组件调用 actions 中的方法(dispatch),通过 actions 中的方法调用 mutations 中的方法(commit),通过 mutations 中的方法去操作 state 中的笔记列表(notes),当前笔记(activenote)等等
一、index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>vuex-note</title>
</head>
<body>
<p id="app"></p>
<!-- built files will be auto injected -->
</body>
</html>
这个没什么说的,注意 p 的 id 就行
二、main.js
import vue from 'vue'
import app from './app'
import store from './store'
vue.config.productiontip = false
new vue({
el: '#app',
store,
components: { app },
template: '<app/>'
})
1.在 import 时什么时候需要 ' ./ '?
从项目模块中导出,引入时不需要 ./,而从自己写的组件中引入时需要 ./
2.什么时候需要 import {aaa} from abc 这种加大括号的引入?什么时候不需要?
当 abc 中被导出的部分是 export aaa 时
当 import 的是被 export default 导出的部分时不加 {},并且可以起个别名
3.项目结构中并没有 store 文件,只有 store 文件夹,那 import store from './store' 是什么意思?
不知道,求指教
4. new vue 中单独的 store 是什么意思?
es6 的一种简写方式,缩写之前是 store:store,这句话的意思是为全局注入 vuex,这样在各个组件中都可以通过 this.$store 去调用状态库,如果不在全局注入,则需要在每个组件中单独引入,多了会很麻烦
三、store 下的 index.js
import vue from 'vue'
import vuex from 'vuex'
import getters from './getters'
import mutations from './mutations'
import actions from './actions'
vue.use(vuex)
const defaultnote = {
id: +new date(),
title: '新建笔记' + new date().getmilliseconds(), // 加时间是为了做一下区分
content: '笔记内容',
fav: false
}
// 可以理解为一个状态的仓库
const state = {
notes: [defaultnote], // 以数组方式存放所有的笔记
activenote: defaultnote, // 用来记录当前笔记
show: 'all' // 用于切换 全部 / 已收藏 两种不同列表的标识
}
export default new vuex.store({
state,
getters,
mutations,
actions
})
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>
<p id="tool">
<button class="add" @click="add_note">新增</button>
<button class="fav" @click="fav_note">收藏</button>
<button class="del" @click="del_note">删除</button>
</p>
</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
<template>
<p id="list">
<p class="switch">
<button class="all" @click='get_switch_note("all")'>全部</button><button class="fav" @click='get_switch_note("fav")'>已收藏</button>
</p>
<p class="search">
<input type="text" placeholder="在这里搜索" v-model="search" />
</p>
<p class="notelist">
<p class="note" v-for="note in search_filterednote" :class="{favcolor:note.fav===true,active:note.id===activenote.id}" @click='get_select_note(note)'>
<p class="title">
<p>{{note.title}}</p>
</p>
<p class="content">
<p>{{note.content}}</p>
</p>
</p>
</p>
</p>
</template>
<script type="text/javascript">
import { mapstate, mapgetters, mapactions } from 'vuex'
export default {
name: 'list',
data: function() {
return {
search: ""
}
},
computed: {
...mapstate(['notes', 'activenote']),
...mapgetters(['filterednote']),
// 二次过滤:在当前列表(全部 或 已收藏)中进行筛选,返回值被用在组件的 v-for 中
search_filterednote() {
if(this.search.length > 0) { // 如果输入框有值,返回二次过滤的结果并加载
return this.filterednote.filter(note => {
if(note.title.indexof(this.search) > 0) {
return note
}
})
} else { // 输入框没值,不过滤,直接拿来加载
return this.filterednote
}
}
},
methods: {
...mapactions(['select_note', 'switch_note']),
get_select_note(note) {
this.select_note(note)
},
get_switch_note(type) {
this.switch_note(type)
}
}
}
</script>
<style type="text/css" scoped="scoped">
#list {
width: 300px;
height: 600px;
border: 2px solid #ccc;
float: left;
margin-left: 10px;
display: flex;
flex-direction: column;
}
p {
margin: 0;
}
.switch {}
.switch button {
height: 60px;
width: 50%;
font-size: 40px;
}
.search {
border: 1px solid #cccccc
}
input {
width: 100%;
box-sizing: border-box;
height: 50px;
line-height: 50px;
padding: 10px;
outline: none;
font-size: 20px;
border: none;
}
.notelist {
flex-grow: 1;
overflow: auto;
}
.note {
border: 1px solid #cccccc;
}
.favcolor {
background: pink;
}
.active {
background: lightblue
}
</style>
1.data 中的 search 是干嘛的?可不可以写在 computed 中?
用来与搜索框进行关联。可以写在 computed 中,但 computed 中的属性默认都是 getter ,就是只能获取值,如果想修改,需要设置 setter ,详见官方文档
六、edit.vue
<template>
<p id="edit">
<p class="title">
<input type="text" placeholder="在这里输入标题" v-model="activenote.title"/>
</p>
<p class="content">
<textarea name="" placeholder="在这里吐槽" v-model="activenote.content"></textarea>
</p>
</p>
</template>
<script type="text/javascript">
import { mapstate, mapgetter, mapactions } from 'vuex'
export default {
name: 'edit',
computed:{
...mapstate(['activenote']) // 当本组件中 computed 中的属性名与 vuex 中的 state 属性名相同时,就可以在 mapstate() 中简写
}
}
</script>
<style type="text/css" scoped="scoped">
#edit {
width: 300px;
height: 600px;
border: 2px solid #ccc;
float: left;
margin-left: 10px;
display: flex;
flex-direction: column;
}
.title {
border: 1px solid #cccccc;
}
input {
width: 100%;
box-sizing: border-box;
height: 50px;
line-height: 50px;
padding: 10px;
outline: none;
font-size: 20px;
border: none;
}
.content {
flex-grow: 1;
background: orange;
display: flex;
flex-direction: column;
}
textarea {
width: 100%;
box-sizing: border-box;
flex-grow: 1;
resize: none;
padding: 10px;
font-size: 20px;
outline: none;
font-family: inherit;
}
</style>
七、actions.js
export default {
add_note({commit}) {
commit('add_note')
},
select_note({commit}, note) {
commit("select_note", note)
},
del_note({commit}) {
commit("del_note")
},
fav_note({commit}) {
commit("fav_note")
},
switch_note({commit}, type) {
commit("switch_note", type)
}
}
1.这是干什么?
这里的每个方法实际上是通过 commit 调用 mutations.js 中的方法;
举个栗子:tool.vue 的 新增 按钮上绑了一个 add_note 自定义方法,在 actions.js 中也定义一个同名的方法,这样就可以在 tool.vue 中的 mapactions 中简写,就是下面这句:
# tool.vue
...mapactions(['add_note','del_note','fav_note'])
而 actions.js 中的 add_note 去调用 mutations.js 中写好的 add_note 方法,而实际的添加操作也是在 add_note 中,组件也好,actions 也好,最终只是调用 add_note 。之所以这么做是因为 mutations 中的方法都是同步的,而 actions 中的方法是异步的,不过在本例里没啥区别
八、getters.js
export default {
filterednote: (state) => {
if(state.show === 'all') {
return state.notes
} else {
return state.notes.filter((note) => {
if(note.fav === true) {
return note
}
})
}
}
}
实现一个过滤,根据 show 来判断展示 全部笔记 还是 已收藏笔记
九、mutations.js
import { switch_note, add_note, select_note, del_note, fav_note } from './mutation-types'
export default {
[add_note](state, note = {
id: +new date(),
title: '新建笔记' + new date().getmilliseconds(),
content: '笔记内容',
fav: false
}) {
state.notes.push(note)
state.activenote = note
},
[select_note](state, note) {
state.activenote = note
},
[del_note](state) {
for(let i = 0; i < state.notes.length; i++) {
if(state.notes[i].id === state.activenote.id) {
state.notes.splice(i, 1)
state.activenote = state.notes[i] || state.notes[i - 1] || {}
return
}
}
},
[fav_note](state) {
state.activenote.fav = !state.activenote.fav
},
[switch_note](state, type) {
state.show = type
}
}
1.export default 那里看着好熟悉
es6 函数的一种写法,中括号 + 常量 作为函数名,这里常量从其它文件引入
十、mutation-types.js
export const add_note = "add_note"
export const select_note = "select_note"
export const del_note = "del_note"
export const fav_note = "fav_note"
export const switch_note = "switch_note"
抛出常量,mutations.js 中的函数常量就是这里抛出的,查资料说是这么做便于一目了然都有那些方法。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
分析javascript原型及原型链
jquery中each方法的使用详解
jquery 实现拖动文件上传加载进度条功能
以上就是使用vuex实现一个笔记应用的方法的详细内容。