使用 Vuex + Vue.js 构建单页应用【新篇】
使用 Vuex + Vue.js 构建单页应用【新篇】
在去年的七月六号的时候,发布了一篇 使用 Vuex + Vue.js 构建单页应用 的文章,文章主要是介绍
vuex
的基本使用方法,发现对大部分的入门同学有很大的帮助,时至今日还有很多同学不断的点赞与收藏,浏览量最高达到 20.4K 。鉴于前端技术发展更新快速,特此在这里重新整理一篇 vue2.0 版本的vuex
基本使用方法,希望能给更多刚入门或者将要入门的同学带来帮助。
这篇文章主要是介绍最新 vue2.0 API 的使用方法, 和 vue1.x 的一些差异的地方。
阅读建议
1、在阅读这篇文章之前,我希望你已经阅读过我上一篇文章 使用 Vuex + Vue.js 构建单页应用 ,明白我们需要实现的基本需求。
2、希望你阅读并掌握以下知识点
目录层级变化
首先是目录层级的变动,我们看下前后对比:
2.0 版本,vuex 的文件夹改为了 store
├── index.html
├── src
│ ├── App.vue
│ ├── assets
│ │ └── logo.png
│ ├── components
│ │ ├── Editor.vue
│ │ ├── NoteList.vue
│ │ └── Toolbar.vue
│ ├── main.js
│ └── store
│ ├── actions.js
│ ├── getters.js
│ ├── index.js
│ ├── mutation-types.js
│ └── mutations.js
└── static
1..0 版本
├── index.html
├── src
│ ├── App.vue
│ ├── assets
│ │ └── logo.png
│ ├── components
│ │ ├── Editor.vue
│ │ ├── NotesList.vue
│ │ └── Toolbar.vue
│ ├── main.js
│ └── vuex
│ ├── actions.js
│ ├── getters.js
│ └── store.js
└── static
使用方式的变动
在文件的 main.js
中注入,2.0 的注入方式如下
import Vue from 'vue'
import App from './App'
import store from './store';
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
store,
components: { App }
})
在组件中使用方式
我们来看 Editor.vue
组件内部如何使用 vuex
的
<template>
<div class="note-editor">
<div class="form-group">
<input type="text" name="title"
class="title form-control"
placeholder="请输入标题"
@input="updateNote"
v-model="currentNote.title">
<textarea
v-model="currentNote.content" name="content"
class="form-control" row="3" placeholder="请输入正文"
@input="updateNote"></textarea>
</div>
</div>
</template>
<style>
...
</style>
<script>
import { mapState, mapActions, mapGetters } from 'vuex';
export default {
name: 'Editor',
computed: {
...mapGetters([
'activeNote'
]),
currentNote() {
return this.activeNote;
}
},
methods: {
...mapActions({
update: 'updateNote'
}),
updateNote() {
this.update({
note: this.currentNote
});
}
}
}
</script>
由于我们在入口文件 main.js
中已经注入 store
对象,使得我们能够在子组件中获取到它,在这里,我们使用了 vuex
提供的三个扩展方法 mapState
、mapActions
、mapGetters
。
另外一个不同点是在我们的 NodeList.vue
组件中,在 vue2.0
里面已经移除了自带的过滤器函数,官方建议我们使用计算属性,下面是我们更改后的使用方法:
<template>
<div class="notes-list">
<div class="list-header">
<h2>Notes | heavenru.com</h2>
<div class="btn-group btn-group-justified" role="group">
<!-- all -->
<div class="btn-group" role="group">
<button type="button" class="btn btn-default"
@click="toggleShow('all')"
:class="{active: show === 'all'}">All Notes</button>
</div>
<!-- favorites -->
<div class="btn-group" role="group">
<button type="button" class="btn btn-default"
@click="toggleShow('favorite')"
:class="{active: show === 'favorite'}">Favorites</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group">
<div class="input-group search">
<input type="text" class="form-control" v-model="search" placeholder="Search for...">
<span class="input-group-addon">
<i class="glyphicon glyphicon-search"></i>
</span>
</div>
</div>
</div>
<!-- 渲染笔记列表 -->
<div class="container">
<div class="list-group">
<div
v-for="(note, index) in searchNotes" :key="index"
class="list-group-item"
:class="{active: activeNote === note}"
@click="updateActiveNote(note)">
<h4 class="list-group-item-heading">
{{note.title.trim().substring(0,30)}}
</h4>
</div>
</div>
</div>
</div>
</template>
<style scoped>
...
</style>
<script>
import { mapGetters, mapState, mapActions } from 'vuex';
export default {
name: 'NoteList',
data() {
return {
search: ''
}
},
computed: {
...mapGetters([
'filteredNotes'
]),
// state 内部状态
...mapState([
'show',
'activeNote'
]),
// 计算属性,自定义过滤
searchNotes() {
if (this.search.length > 0) {
return this.filteredNotes.filter((note) => note.title.toLowerCase().indexOf(this.search) > -1);
} else {
return this.filteredNotes;
}
}
},
methods: {
...mapActions([
'toggleListShow',
'setActiveNote'
]),
// 切换列表,全部或者收藏
toggleShow(type) {
this.toggleListShow({ show: type});
},
// 点击列表,更新当前激活文章
updateActiveNote(note) {
this.setActiveNote({ note });
}
}
}
</script>
Q&A
其他的变动,大家自行的查看源码学习:vuex-notes-app2。
使用 Vuex + Vue.js 构建单页应用【新篇】的更多相关文章
- 使用 Vuex + Vue.js 构建单页应用
鉴于该篇文章阅读量大,回复的同学也挺多的,特地抽空写了一篇 vue2.0 下的 vuex 使用方法,传送门:使用 Vuex + Vue.js 构建单页应用[新篇] ------------------ ...
- 通过Web Api 和 Angular.js 构建单页面的web 程序
通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...
- Web API 2 入门——使用ASP.NET Web API和Angular.js构建单页应用程序(SPA)(谷歌翻译)
在这篇文章中 概观 演习 概要 由网络营 下载网络营训练包 在传统的Web应用程序中,客户机(浏览器)通过请求页面启动与服务器的通信.然后,服务器处理请求,并将页面的HTML发送给客户端.在与页面的后 ...
- 基于VUE的SPA单页应用开发-加载性能篇
1.基于异步数据的vue页面刷新 先看看基于异步数据的vue页面刷新后,都发生了啥- 如图所示: 图1 基于异步数据的vue页面刷新 网络请求图 步骤如下: step1:请求页面: step2:请求页 ...
- vue2.0 构建单页应用最佳实战
vue2.0 构建单页应用最佳实战 前言 我们将会选择使用一些 vue 周边的库vue-cli, vue-router,vue-resource,vuex 1.使用 vue-cli 创建项目2.使 ...
- React构建单页应用方法与实例
React作为目前最流行的前端框架之一,其受欢迎程度不容小觑,从这门框架上我们可以学到许多其他前端框架所缺失的东西,也是其创新性所在的地方,比如虚拟DOM.JSX等.那么接下来我们就来学习一下这门框架 ...
- 借助 Vue 来构建单页面应用
原文: https://github.com/MeCKodo/vue-tutorial 主题 Vue.js (1/2)Vue构建单页应用最佳实战 前言 我们将会选择使用一些vue周边的库 1.使用no ...
- Nodejs in Visual Studio Code 12.构建单页应用Scrat实践
1.开始 随着前端工程化深入研究,前端工程师现在碉堡了,甚至搞了个自己的前端网站http://div.io/需要邀请码才能注册,不过里面的技术确实牛.距离顶级的前端架构,目前博主应该是far away ...
- require.js实现单页web应用(SPA)
本文转载自:https://blog.csdn.net/qq_33401924/article/details/53815922 移动端单页应用基本上是做移动端最流行的的方式了,但是对于很多前端来说, ...
随机推荐
- C#comboBox取消SelectedIndexChanged事件
问题: comboBox在加载数据源时会默认引发SelectedIndexChanged事件. 解决: 在comboBox加载数据源时添加如下代码 cmbGroupName.SelectedIndex ...
- Dubbo服务如何优雅的校验参数
一.背景 服务端在向外提供接口服务时,不管是对前端提供HTTP接口,还是面向内部其他服务端提供的RPC接口,常常会面对这样一个问题,就是如何优雅的解决各种接口参数校验问题? 早期大家在做面向前端提供的 ...
- SQL Server--设置用IP地址登录
问题概述:新安装的SQL Server数据库无法用IP地址登录. 是因为 SQL Server "服务器网络实用工具"中禁用了"命名管道"所致! 在sqlse ...
- Laravel自定义错误提示,自定义异常类提示,自定义错误返回信息,自定义错误页面
方法一 新增CustomException.php文件 App\Exceptions\CustomException.php <?php namespace App\Exceptions; us ...
- VUE-表单验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- laravel报错 : No application encryption key has been specified.
创建了新的laravel项目后, 运行提示:No application encryption key has been specified 解决方法: 这个是由于没有配置好 APP_KEY 在终端上 ...
- [] == ![] 返回 true
对于==来说,如果数据类型不同,就会进行隐式类型转换. 首先判断是否在对比 null 和 undefined,是的话就会返回 true: 判断其中一方是否为 string ,在与 number进行比较 ...
- LGP7814题解
lmpp 教你对着样例得到做法.jpg 题意:给定一个长度为 $ n $ 的字符串 A,要求你构造一个字符串 B,使得 A 是 B 的子序列且 A 不是 B 的子串. 首先给出无解的判断方法: if( ...
- BUU [GKCTF 2021]签到
BUU [GKCTF 2021]签到 1.题目概述 2.解题过程 追踪HTTP流 在下面发现了一串可疑字符 Base16转base64 放到010里看看 复制下来,去转字符 好像不是,再回去找找其他的 ...
- Java——spring中session的获取
获得session public static HttpServletRequest getSession(){ return ((ServletRequestAttributes) RequestC ...