vueX

什么是vueX?

官方文档
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化

如何引用?

准备工作

js引入方式
<script src="/path/to/vue.js"></script>
<script src="/path/to/vuex.js"></script>
npm引入方式
npm install vuex --save
模块化的打包系统中
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

1、创建index.js文件

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
导入对应的组件状态管理文件
import State from './state';
统一在vuex的store对象中管理
const store = new Vuex.Store(State);
导出
export default store;

2、创建state.js文件

export default {
	state: {
		test: 0,
	
	},
	mutations:{ 
		'test': function(state, value) {
			state.test = value;
		}
	},
	getters:{
		'test': function(state) {
			return state.test;
		}
	}
};

--提交值
this.$store.commit('key',更新的内容);
--获取值 用变量进行接受
this.$store.getters.key  

3、引入项目中

在vue入口中引入store文件
import store from './store';
根组件注入store 的机制
new Vue({
  el: '#app',
  store: store,
});

state 单一状态树 当前状态

单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。
Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法就是在计算属性 (opens new window)中返回某个状态:

// 创建一个 Counter 组件
const Counter = {
  template: `<div>{{ count }}</div>`,
  computed: {
    count () {
      return store.state.count
    }
  }
}
在store.js中:
state: {
		test: 0,
	},

每当 store.state.count 变化的时候, 都会重新求取计算属性,并且触发更新相关联的 DOM。
Vue.use(Vuex):通过调用此vuex 外,在根组件中引入store,可以注入到每一个子组件中。子组件就能实时得到相应的数据

mapState:组件需要获取多个状态,生成计算属性,函数返回的是一个对象。
computed:监控自己定义的百年来那个,改变量不在data中定义,直接在computed中定义,最好的场景就是页面动态显示的结果,可以放到此处进行计算监控和显示

computed: {
  localComputed () { /* ... */ },
  // 使用对象展开运算符将此对象混入到外部对象中
  ...mapState({
    // ...
  })
}

Getter 获取状态

就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算

getters: {
    test:function(state){
	  return state.test;
	}
  }

this.$store.getters.test;

Mutation 提交状态

这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数:

提交载荷(Payload)

可以向 store.commit 传入额外的参数,即 mutation 的 载荷(payload)

使用常量替代 Mutation 事件类型

使用文件单独放置常量,来进行系统引用
export const SOME_MUTATION = ‘SOME_MUTATION’

Mutation 必须是同步函数

mutations:{
		'test': function(state, value) {
			state.test = value;
		}
	}
	this.$store.commit('test',内容);
	

Action

Action 提交的是 mutation,而不是直接变更状态。
Action 可以包含任意异步操作。

Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation

actions: {
  increment ({ commit }) {
    commit('test')
  }
}

分发 Action

Action 通过 store.dispatch 方法触发

store.dispatch('increment')

Actions 支持同样的载荷方式和对象方式进行分发:

// 以载荷形式分发
store.dispatch('test', {
  amount: 10
})

// 以对象形式分发
store.dispatch({
  type: 'test',
  amount: 10
})

在组件中分发 Action

import { mapActions } from 'vuex'

export default {
  // ...
  methods: {
    ...mapActions([
      'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`

      // `mapActions` 也支持载荷:
      'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
    })
  }
}

组合 Action

actions: {
  async actionA ({ commit }) {
    commit('gotData', await getData())
  },
  async actionB ({ dispatch, commit }) {
    await dispatch('actionA') // 等待 actionA 完成
    commit('gotOtherData', await getOtherData())
  }
}

Module模块

为了解决store 对象变得相当臃肿问题,vuex允许我们将store分割成模块,每个模块都有自己的state、mutation、action、getter、子嵌套模块

const moduleA = {
  state: () => ({ ... }),
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: () => ({ ... }),
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态

命名空间

加入namespaced: true 开启命名空间

const store = new Vuex.Store({
  modules: {
    account: {
      namespaced: true,

      // 模块内容(module assets)
      state: () => ({ ... }), // 模块内的状态已经是嵌套的了,使用 `namespaced` 属性不会对其产生影响
      getters: {
        isAdmin () { ... } // -> getters['account/isAdmin']
      },
      actions: {
        login () { ... } // -> dispatch('account/login')
      },
      mutations: {
        login () { ... } // -> commit('account/login')
      },

      // 嵌套模块
      modules: {
        // 继承父模块的命名空间
        myPage: {
          state: () => ({ ... }),
          getters: {
            profile () { ... } // -> getters['account/profile']
          }
        },

        // 进一步嵌套命名空间
        posts: {
          namespaced: true,

          state: () => ({ ... }),
          getters: {
            popular () { ... } // -> getters['account/posts/popular']
          }
        }
      }
    }
  }
})

以下分为两种使用情况
在带命名空间的模块内访问全局内容(Global Assets)

此处不是太理解…后面视频学习

在带命名空间的模块注册全局 action

root: true, 开启当前模块action全局模式

{
  actions: {
    someOtherAction ({dispatch}) {
      dispatch('someAction')
    }
  },
  modules: {
    foo: {
      namespaced: true,

      actions: {
        someAction: {
          root: true,
          handler (namespacedContext, payload) { ... } // -> 'someAction'
        }
      }
    }
  }
}

调试日志加载、开启严格模式、

仅供开发环境使用

import createLogger from 'vuex/dist/logger'

日志:plugins: [createLogger()]
严格模式:strict: true

const store = new Vuex.Store(
{plugins: [createLogger()],State,strict: true});

测试mutation、测试 Action、测试 Getter

需要插件和专属配置支持、比较麻烦

热重载

需要支持 mutation 和模块,你需要使用 store.hotUpdate() 方法:

// store.js
import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import moduleA from './modules/a'
Vue.use(Vuex)

const state = { ... }

const store = new Vuex.Store({
  state,
  mutations,
  modules: {
    a: moduleA
  }
})

if (module.hot) {
  // 使 action 和 mutation 成为可热重载模块
  module.hot.accept(['./mutations', './modules/a'], () => {
    // 获取更新后的模块
    // 因为 babel 6 的模块编译格式问题,这里需要加上 `.default`
    const newMutations = require('./mutations').default
    const newModuleA = require('./modules/a').default
    // 加载新模块
    store.hotUpdate({
      mutations: newMutations,
      modules: {
        a: newModuleA
      }
    })
  })
}

在此声明,以上内容部分列子来于官方文档

SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知青先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值