路由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. 配置

    1. {
    2. state: {
    3. cart: localStorage.getItem('cart')
    4. },
    5. mutations:{
    6. addCart:(state)=>{
    7. }
    8. },
    9. getter:{},
    10. actions:{}
    11. }
  2. 使用

    • commit()
    • dispatch()
    • $store.state.xx

vuex

  1. class KVuex {
  2. constructor (options) {
  3. this.state = options.state
  4. this.mutations = options.mutations
  5. this.actions = options.actions
  6. // 借用vue本身的响应式的通知机制
  7. // state 会将需要的依赖收集在 Dep 中
  8. this._vm = new KVue({
  9. data: {
  10. $state: this.state
  11. }
  12. })
  13. }
  14. commit (type, payload, _options) {
  15. const entry = this.mutations[type]
  16. entry.forEach(handler=>handler(payload))
  17. }
  18. dispatch (type, payload) {
  19. const entry = this.actions[type]
  20. return entry(payload)
  21. }
  22. }

github.com/vuejs/vuex

vue-router

使用

  1. const routes = [
  2. { path: '/', component: Home },
  3. { path: '/book', component: Book },
  4. { path: '/movie', component: Movie }
  5. ]
  6. const router = new VueRouter(Vue, {
  7. routes
  8. })
  9. new Vue({
  10. el: '#app',
  11. router
  12. })
  1. class VueRouter {
  2. constructor(Vue, options) {
  3. this.$options = options
  4. this.routeMap = {}
  5. this.app = new Vue({
  6. data: {
  7. current: '#/'
  8. }
  9. })
  10. this.init()
  11. this.createRouteMap(this.$options)
  12. this.initComponent(Vue)
  13. }
  14. // 初始化 hashchange
  15. init() {
  16. window.addEventListener('load', this.onHashChange.bind(this), false)
  17. window.addEventListener('hashchange', this.onHashChange.bind(this), false)
  18. }
  19. createRouteMap(options) {
  20. options.routes.forEach(item => {
  21. this.routeMap[item.path] = item.component
  22. })
  23. }
  24. // 注册组件
  25. initComponent(Vue) {
  26. Vue.component('router-link', {
  27. props: {
  28. to: String
  29. },
  30. template: '<a :href="to"><slot></slot></a>'
  31. })
  32. const _this = this
  33. Vue.component('router-view', {
  34. render(h) {
  35. var component = _this.routeMap[_this.app.current]
  36. return h(component)
  37. }
  38. })
  39. }
  40. // 获取当前 hash 串
  41. getHash() {
  42. return window.location.hash.slice(1) || '/'
  43. }
  44. // 设置当前路径
  45. onHashChange() {
  46. this.app.current = this.getHash()
  47. }
  48. }

状态管理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. django创建分页

    前台html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. FastReport二维码打印存在的问题

    FastReport二维码打印存在的问题 (2018-05-21 09:28:38) 转载▼ 标签: delphi 分类: Delphi10.2 FastReport本身支持二维码,实际应用中遇到这样 ...

  3. Delphi IDHTTP控件:GET/POST 请求

    Delphi IDHTTP控件:GET/POST 请求   最近一直在使用IDHTTP,下面是一些关于 GET.POST 请求基本使用方法的代码 一.GET 请求 1 procedure GetDem ...

  4. 撸.NET Core的正确姿势

    特点 案例基于刚发布的.NET Core 2.1 只需一台Linux服务器搞定一切, 全程无需自己配置dotnet环境, 需要熟悉git docker基础知识可有可无, 过了下面几个步骤,你就已经入门 ...

  5. 解决网页出现 net::ERR_ABORTED 404 (Not Found)问题

    1.在web.config配置文件中添加woff字体的MIME类型 在Web.config中的system.webServer节点添加 <staticContent> <remove ...

  6. 使用C#写MVC框架(一:核心原理)

    目录: 一.MVC原理解析 二.HttpHandler 1.HttpHandler,IHttpHandler,MvcHandler的说明 2.IHttpHandler解析 3.MvcHandler解析 ...

  7. Java并发编程总结3——AQS、ReentrantLock、ReentrantReadWriteLock

    本文内容主要总结自<Java并发编程的艺术>第5章——Java中的锁. 一.AQS AbstractQueuedSynchronizer(简称AQS),队列同步器,是用来构建锁或者其他同步 ...

  8. [转载]Java并发编程:深入剖析ThreadLocal

                原文地址:http://www.cnblogs.com/dolphin0520/p/3920407.html 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨 ...

  9. C# 生成月份及天选择列表,方便做下拉框联动

    月份及天选择列表,很方便做下拉框联动 /// <summary> /// 获取月份选择列表(根据当前语言环境显示月份名称) /// </summary> private IEn ...

  10. 爬虫 解码gb1312类型

    今天我爬虫的时候竟然遇见了网页编码为gb1312类型的网站  , 不是平常的utf-8 遇到这种类型的时候忽视它是最好的办法 ① respond.content.decode('gb18030','i ...