状态管理Vuex
路由Router
- 配置 {path:'/login',component:Login}
- 路由出口 router-view
- 传参
- {path:'/login/:id',component:Login} $route.params.id
- ?foo=aaa $route.query.foo
- 守卫
- 全局 router.beforeEach
- 路由beforeEnter
- 组件beforeRouteEnter
- 嵌套 {children:[]}
状态管理Vuex
配置
{
state: {
cart: localStorage.getItem('cart')
},
mutations:{
addCart:(state)=>{ }
},
getter:{},
actions:{}
}
使用
- commit()
- dispatch()
- $store.state.xx
vuex

class KVuex {
constructor (options) {
this.state = options.state
this.mutations = options.mutations
this.actions = options.actions
// 借用vue本身的响应式的通知机制
// state 会将需要的依赖收集在 Dep 中
this._vm = new KVue({
data: {
$state: this.state
}
})
}
commit (type, payload, _options) {
const entry = this.mutations[type]
entry.forEach(handler=>handler(payload))
}
dispatch (type, payload) {
const entry = this.actions[type]
return entry(payload)
}
}
github.com/vuejs/vuex
vue-router
使用
const routes = [
{ path: '/', component: Home },
{ path: '/book', component: Book },
{ path: '/movie', component: Movie }
]
const router = new VueRouter(Vue, {
routes
})
new Vue({
el: '#app',
router
})
class VueRouter {
constructor(Vue, options) {
this.$options = options
this.routeMap = {}
this.app = new Vue({
data: {
current: '#/'
}
})
this.init()
this.createRouteMap(this.$options)
this.initComponent(Vue)
}
// 初始化 hashchange
init() {
window.addEventListener('load', this.onHashChange.bind(this), false)
window.addEventListener('hashchange', this.onHashChange.bind(this), false)
}
createRouteMap(options) {
options.routes.forEach(item => {
this.routeMap[item.path] = item.component
})
}
// 注册组件
initComponent(Vue) {
Vue.component('router-link', {
props: {
to: String
},
template: '<a :href="to"><slot></slot></a>'
})
const _this = this
Vue.component('router-view', {
render(h) {
var component = _this.routeMap[_this.app.current]
return h(component)
}
})
}
// 获取当前 hash 串
getHash() {
return window.location.hash.slice(1) || '/'
}
// 设置当前路径
onHashChange() {
this.app.current = this.getHash()
}
}
状态管理Vuex的更多相关文章
- Vue状态管理vuex
前面的话 由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue提供了vuex.本文将详细介绍Vue状态管理vuex 引入 当访问数据对象时,一个 V ...
- Vue之状态管理(vuex)与接口调用
Vue之状态管理(vuex)与接口调用 一,介绍与需求 1.1,介绍 1,状态管理(vuex) Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态 ...
- Vue.js 2.x笔记:状态管理Vuex(7)
1. Vuex简介与安装 1.1 Vuex简介 Vuex是为vue.js应用程序开发的状态管理模式,解决的问题: ◊ 组件之间的传参,多层嵌套组件之间的传参以及各组件之间耦合度过高问题 ◊ 不同状态中 ...
- vue创建状态管理(vuex的store机制)
1:为什么说要是永远状态管理 在使用 Vue 框架做单页面应用时,我们时常会遇到传值,组件公用状态的问题.(子父间传值文章传送门) ,如果是简单的应用,兄弟组件之间通信还能使用 eventBus 来作 ...
- Vue-认识状态管理vuex
vuex是一个专门为vue.js设计的状态管理模式,并且也可以使用devtools进行调试,可以多个组件共享状态.简单来说,就是共享的状态用state来存放,用mutations来操作state,但是 ...
- vue总结 08状态管理vuex
状态管理 类 Flux 状态管理的官方实现 由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue 提供 vuex:我们有受到 Elm 启发的状态管 ...
- 状态管理(Vuex、 Flux、Redux、The Elm Architecture)
1.https://vuex.vuejs.org/zh-cn/intro.html (vuex) 这就是 Vuex 背后的基本思想,借鉴了 Flux.Redux.和 The Elm Architect ...
- vue中状态管理vuex的使用分享
一.main.js中引入 store import store from './store' window.HMO_APP = new Vue({ router, store, render: h = ...
- Vue学习日记(四)——Vue状态管理vuex
前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...
随机推荐
- Android中去掉标题栏
在Android中去掉标题栏有三种方法,它们也有各自的特点. 1.在代码里实现 this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 记 ...
- SystemProperties.get/set property_get/set
在java层设置系统属性要注意几点: 1 需要有系统权限.可以在AndroidManifest.xml添加android:sharedUserId="android.uid.system&q ...
- AngularJS $watch 性能杀手
双向绑定是AngularJS核心概念之一,它给我们带来了思维的转变,不再是以DOM为驱动,而是以Model为核心,View中写上声明式标签(指令或{{}}),AngularJS会在后台默默同步View ...
- BAE上部署Ghost 0.5.1注意事项
BAE上部署Ghost可参考基本安装上述安装使用的是ghost0.4.7版本 在ghost 0.5 中为了解决测试时事件侦听器过多引发的警告,在注册single事件时,将代码由原先的 process. ...
- dotNet Core WEB程序使用 Nginx反向代理
之前记录过一篇 使用 jexus 作为dotNetCore的反向代理,发现jexus的内存占用较大,最终选择使用Nginx的原因就是占用内存较小,以及性能较优(https://www.cnblogs. ...
- C# 读取资源文件.resx 中的xml资源
主要是以字符串的形式来读取xml,然后通过遍历读取节点,通过节点属性名称获取属性值 /// <summary> /// 初始化OPC参数配置 /// </summary> // ...
- EF Core 迁移过程遇到EF Core tools version版本不相符的解决方案
如果你使用命令: PM> add-migration Inital 提示如下信息时: The EF Core tools version '2.1.1-rtm-30846' is older t ...
- Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- ssh远程登录出现Host key verification failed.解决办法
今天通过ssh和域名连接主机: IcarusdeMacBook-Pro:~ icarus$ ssh root@icarusyu.me 出现了如下错误: @@@@@@@@@@@@@@@@@@@@@@@@ ...
- cisco 的ACL
搞网络好几年了,怎么说呢,水平一直停留在NA-NP之间,系统的学完NA后,做了不少实验,后来也维护了企业的网络,各种网络设备都玩过(在商汤用的Juniper srx 550 我认为在企业环境,非IDC ...