路由Router

  1. 配置 {path:'/login',component:Login}
  2. 路由出口 router-view
  3. 传参
    • {path:'/login/:id',component:Login} $route.params.id
    • ?foo=aaa $route.query.foo
  4. 守卫
    • 全局 router.beforeEach
    • 路由beforeEnter
    • 组件beforeRouteEnter
  5. 嵌套 {children:[]}

状态管理Vuex

  1. 配置

    {
    state: {
    cart: localStorage.getItem('cart')
    },
    mutations:{
    addCart:(state)=>{ }
    },
    getter:{},
    actions:{}
    }
  2. 使用

    • 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的更多相关文章

  1. Vue状态管理vuex

    前面的话 由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue提供了vuex.本文将详细介绍Vue状态管理vuex 引入 当访问数据对象时,一个 V ...

  2. Vue之状态管理(vuex)与接口调用

    Vue之状态管理(vuex)与接口调用 一,介绍与需求 1.1,介绍 1,状态管理(vuex) Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态 ...

  3. Vue.js 2.x笔记:状态管理Vuex(7)

    1. Vuex简介与安装 1.1 Vuex简介 Vuex是为vue.js应用程序开发的状态管理模式,解决的问题: ◊ 组件之间的传参,多层嵌套组件之间的传参以及各组件之间耦合度过高问题 ◊ 不同状态中 ...

  4. vue创建状态管理(vuex的store机制)

    1:为什么说要是永远状态管理 在使用 Vue 框架做单页面应用时,我们时常会遇到传值,组件公用状态的问题.(子父间传值文章传送门) ,如果是简单的应用,兄弟组件之间通信还能使用 eventBus 来作 ...

  5. Vue-认识状态管理vuex

    vuex是一个专门为vue.js设计的状态管理模式,并且也可以使用devtools进行调试,可以多个组件共享状态.简单来说,就是共享的状态用state来存放,用mutations来操作state,但是 ...

  6. vue总结 08状态管理vuex

      状态管理 类 Flux 状态管理的官方实现 由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue 提供 vuex:我们有受到 Elm 启发的状态管 ...

  7. 状态管理(Vuex、 Flux、Redux、The Elm Architecture)

    1.https://vuex.vuejs.org/zh-cn/intro.html (vuex) 这就是 Vuex 背后的基本思想,借鉴了 Flux.Redux.和 The Elm Architect ...

  8. vue中状态管理vuex的使用分享

    一.main.js中引入 store import store from './store' window.HMO_APP = new Vue({ router, store, render: h = ...

  9. Vue学习日记(四)——Vue状态管理vuex

    前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...

随机推荐

  1. codeforces 982 c

    给你一棵树 让你进行切割 问你最多能切多少刀   使得每个连通分量size都是偶数 思路:首先  要是有奇数个节点的话   那么不管你怎么切割  都会有一个连通分量的size是奇数 所以只有偶数的情况 ...

  2. hdu 5084 前缀和预处理

    http://acm.hdu.edu.cn/showproblem.php?pid=5084 给出矩阵M,求M*M矩阵的r行c列的数,每个查询跟前一个查询的结果有关. 观察该矩阵得知,令ans = M ...

  3. Legal or Not HDU

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. 11.js循环与函数

    Switch语句 Switch(变量){ : 如果变量和1的值相同,执行该处代码 break; : 如果变量和2的值相同,执行该处代码 break; : 如果变量和3的值相同,执行该处代码 break ...

  5. unigui编译路径设置

    unigui编译路径设置 先设路径变量 再追加如下路径,即可成功编译: ;$(uni)\uniTools\Dcu\Delphi2021;$(uni)\uniGUI\Dcu\Delphi2021;$(u ...

  6. (25)uniGUI for C++ builder之UniHTMLMemo初使用及uniGUI如何调用javaScript

    (25)uniGUI for C++ builder之UniHTMLMemo初使用及uniGUI如何调用javaScript 2018年09月29日 22:58:20 中国银行之路在脚下 阅读数:11 ...

  7. Android-available for offline mode

    出现 available for offline mode 字样的异常: 取消打钩 Offine work 点击sync project ..... 成功解决:

  8. asp.net web api 跨域问题

    缘起 以前在asp.net mvc时代,很少出现跨域问题 自从使用了asp.net web api + angular (1/2)之后,开始有跨域问题了. 简单普及下跨域: 我的理解是只要是前台页面与 ...

  9. WinForm中实现Loading加载界面

    1,LoaderForm窗体中添加PictureBox,然后添加Loading图片 2,窗体内属性设置 StartPosition :CenterScreen在屏幕中心显示 TopMost:True置 ...

  10. 关于 IPv6

    http://test-ipv6.com/ http://bbs.chinaunix.net/thread-1799798-1-1.html