路由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. 基于MATLAB的腐蚀膨胀算法实现

    本篇文章要分享的是基于MATLAB的腐蚀膨胀算法实现,腐蚀膨胀是形态学图像处理的基础,腐蚀在二值图像的基础上做“收缩”或“细化”操作,膨胀在二值图像的基础上做“加长”或“变粗”的操作. 什么是二值图像 ...

  2. unigui编译路径设置

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

  3. AFNetworking 3.0 AFHTTPSessionManager文件下载

    #import "ViewController.h" #import <AFNetworking.h> @interface ViewController () - ( ...

  4. NET npoi 保存文件

    npoi完整代码:NET npoi帮助类 public static void DataTableToExcel(List<DataTable> dataTables, string fi ...

  5. C# 嵌入dll

    在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形式总归让人不爽,那么有 ...

  6. ASP.Net MVC OA项目笔记<五>

    1.1.1  抽象工厂封装数据操作类实例创建,然后DBSession调用抽象工厂,修改DBSession CZBK.ItcastOA.DALFactory数据会话层调数据层不能直接new,要封装一下解 ...

  7. [leetcode.com]算法题目 - Pow(x, n)

    Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...

  8. scanf的拓展用法——匹配特定字符

    scanf的基本用法除了常规的输入操作外还有一些特殊的用法,使用这些用法可以很方便的在输入中读取想要的数据 1.限制输入数据的长度 这个应该算不上拓展用法,大多数读者应该都曾经使用过,这里简单提一下 ...

  9. 复习 C++ 中类的函数指针

    函数指针这种东西,平时工作中基本上不会用到. 那函数指针会用在哪里? 下面是一些基本的用法,根据消息号调到对应的函数: #include <iostream> #include <m ...

  10. Codeforces 1058 D. Vasya and Triangle(分解因子)

    题目:http://codeforces.com/contest/1058/problem/D 题意:有一个大小为N*M的矩阵内,构造一个三角形,使面积为(n*m)/k.若存在输出三个顶点(整数). ...