Vuex.namespaced 的笔记

命名空间

默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的——这样使得多个模块能够对同一 mutation 或 action 作出响应。

如果希望你的模块具有更高的封装度和复用性,你可以通过添加 namespaced: true 的方式使其成为带命名空间的模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。

namespaced: true


  • 原文: state: { … }, // 模块内的状态已经是嵌套的了,使用 “namespaced” 属性不会对其产生影响
  1. modules.name.state 是嵌套的;
  2. namespaced 不影响 modules.name.state;
  3. modules.name.state 必然是分区的;
  • modules.name 会覆盖 state.name

<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Vuex.namespaced 的笔记</title>
		<style></style>
	</head>

	<body>

		<div id="swq">
			<swq></swq>
		</div>

		<script type="text/x-template" id="swq-template">
			<div></div>
		</script>

		<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
		<script src="http://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script>

		<script type="text/javascript">
			function Consoled(a, b) {
				console.log(a + " --consoled-- " + b)
			}
			const store = new Vuex.Store({
				modules: {
					modA: {
						namespaced: true,
						state: {
							theme: "modA-theme",
						},
						mutations: {
							mutB(state, _val) {
								Consoled("modA/mutB", _val)
								state.theme = _val
							},
						},
						actions: {
							actB(context, _val) {
								var _this = this
								Consoled("modA/actB", _val)
								return new Promise((resolve, reject) => {
									setTimeout(function() {
										_this.commit("modA/mutB", "modA-actB")
										resolve("resolve modA actB")
									}, 1000)
								})
							},
						},
					},
					modB: {
						namespaced: false,
						state: {
							theme: "modB-theme",
						},
						mutations: {
							mutB(state, _val) {
								Consoled("modB/mutB", _val)
								state.theme = _val
							},
						},
						actions: {
							actB(context, _val) {
								var _this = this
								Consoled("modB/actB", _val)
								return new Promise((resolve, reject) => {
									setTimeout(function() {
										_this.commit("mutB", "modB-actB")
										resolve("resolve modB actB")
									}, 1000)
								})
							},
						},
					},
				},
				state: {
					count: 0,
				},
				mutations: {
					mutA(state, _val) {
						Consoled("mod0/mutA", _val)
						state.count++
					},
				},
				getters: {},
				actions: {
					actA(context, _val) {
						var _this = this
						Consoled("mod0/actA", _val)
						return new Promise((resolve, reject) => {
							setTimeout(function() {
								_this.commit("mutA", _val)
								resolve("resolve actA")
							}, 1000)
						})
					},
				},
			})

			/* *** */

			var swq = {
				template: "#swq-template",
				props: [],
				data() {
					return {}
				},
				computed: {}, //计算
				components: {}, //组件
				watch: {}, //看
				methods: {
					//方法
					ctabState(_keys) {
						if(Object.prototype.toString.call(_keys) !== "[object Array]" || _keys.length == 0) {
							return false;
						}
						var _this = this
						var _data = {}

						for(var i = 0; i < _keys.length; i++) {
							var _arr = []
							var _val = _this.$store.state
							var _key = String(_keys[i])

							if(_key.indexOf(".") > -1) {
								_arr = _key.split(".")
							} else {
								_arr.push(_key)
							}

							for(var j = 0; j < _arr.length; j++) {
								_val = _val[_arr[j]]
							}

							//console.log(Object.prototype.toString.call(_val))
							if(Object.prototype.toString.call(_val) == "[object Object]" || Object.prototype.toString.call(_val) == "[object Array]") {
								_val = JSON.stringify(_val)
							}
							_data[_key] = _val
						}
						console.table(_data)
					}
				},
				created() {
					//创建
				},
				mounted() {
					//安装

					var _this = this

					console.log("初始数据, state 必然是分区的, 需要加分区名")
					_this.ctabState(["count", "modA.theme", "modB.theme"])

					/* *** */

					console.log("commit 有两种用法, 都可以带参")
					console.log("mutA 是根mutations 的方法")
					_this.$store.commit("mutA", {
						a: 1
					})
					_this.$store.commit({
						type: "mutA",
						a: 1
					})
					_this.ctabState(["count", "modA.theme", "modB.theme"])

					/* *** */

					console.log("mutB 是 分区 [modA, modB] mutations 的方法")
					console.log("modA 为命名空间, modA.mutB 注册在分区命名空间")
					_this.$store.commit("modA/mutB", "modA/mutB")
					_this.ctabState(["modA.theme", "modB.theme"])
					console.log("modB 没有命名, modB.mutB 注册在全局命名空间")
					_this.$store.commit("mutB", "mutB")
					_this.ctabState(["modA.theme", "modB.theme", "count"])

					/* *** */

				},
			};
			var vu = new Vue({
				el: "#swq",
				store,
				components: {
					swq: swq,
				},
			})
		</script>
	</body>

</html>

参考资料

//end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值