《进阶篇第9章》学习vuex知识点后练习:把求和案例改成vuex模块化编码

news/2024/7/10 0:16:18 标签: vue, vuex, 求和案例, vuex模块化编码

在这里插入图片描述

文章目录

  • 9.7求和案例_vuex模块化编码
    • 完整代码
  • 本人其他相关文章链接

效果展示:

在这里插入图片描述
注意点1:

问题:如何实现“当前和为奇数再加”?

答案:

incrementOdd(){
	if(this.sum % 2){
		this.sum += this.n
	}
}

注意点2:

问题:select下拉框默认为1时,点击加号没问题,但是下拉框选中为2时,n值变成字符串了,不应该是数字类型名吗?

旧代码:无论设置 value="1"还是 value=1都无效

<h1>当前求和为:{{sum}}</h1>
<select v-model="n">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
</select>

答案:因为没设置值选中值为数值类型

方式1,使用v-bind

<h1>当前求和为:{{sum}}</h1>
<select v-model="n">
	<option :value="1">1</option>
	<option :value="2">2</option>
	<option :value="3">3</option>
</select>

方式2,v-model设置修饰符

<h1>当前求和为:{{sum}}</h1>
<select v-model.number="n">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
</select>

vuex_56">9.7求和案例_vuex模块化编码

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意点1:
创建并暴露store有2种写法

写法1完整写法

//创建并暴露store
export default new Vuex.Store({
	modules:{
		countAbout:countOptions,
		personAbout:personOptions
	}
})

写法2简写,不写kye,默认就是key和value同名

//创建并暴露store
export default new Vuex.Store({
	modules:{
		countOptions,
		personOptions
	}
})

注意点2:
报错如图,问题发生在length不认识是哪个对象获得的,解决方案就是如下,mapState第一个参数必须指定分类,不然personList就是underfine,而underfine.length就会报错

<h3 style="color:red">Person组件的总人数是:{{personList.length}}</h3>

...mapState('countAbout',['sum','school','subject']),

在这里插入图片描述
注意点3:

问题:mapState第一个参数指定分类时,报错如图

解决方案:mapState如果想指定分类,那有个前提,在配置vuex时候每个配置里面都加namespaced:true,不配置namespaced默认为false,只有这么写,你的创建并暴露store的modules的名称(也就是countAbout和personAbout),才能被mapState第一个参数认识。
在这里插入图片描述
注意点4:…mapState可以写很多个进行配置

注意点5:

问题:报错如图,就说明没指定这个JIA是哪个分类下面的

解决方案:mapMutations第一个参数也需要指定分类名称,代表从哪里来,正确代码,同理…mapActions第一个参数也需要指定分类名称。

...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'}),

在这里插入图片描述
注意点6:
Count.vue使用map方法封装函数,而Person.vue就用最原始的方法去练习

注意点7:
注意state、getters、dispatch、commit中配置modules的分类名称方法是不一样的,有的方法第一个参数,有的是使用/,识别/前面作为分类名,后面作为mutations的名,详情请看开头总结图片。

注意点8:
在Person.vue中定义的vuex配置项中的state可不是总的state,而是Person.vue它自己的state

注意点9:
读取对象属性使用点“.”的时候就不能使用“/”,

错误写法

firstPersonName(){
	return this.$store.getters.personAbout/firstPersonName
}

正确写法

firstPersonName(){
	return this.$store.getters['personAbout/firstPersonName']
}

完整代码

main.js

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//引入插件
import vueResource from 'vue-resource'
//引入store
import store from './store'

//关闭Vue的生产提示
Vue.config.productionTip = false
//使用插件
Vue.use(vueResource)

//创建vm
new Vue({
	el:'#app',
	render: h => h(App),
	store,
	beforeCreate() {
		Vue.prototype.$bus = this
	}
})

App.vue

<template>
	<div>
		<Count/>
		<hr>
		<Person/>
	</div>
</template>

<script>
	import Count from './components/Count'
	import Person from './components/Person'

	export default {
		name:'App',
		components:{Count,Person},
		mounted() {
			// console.log('App',this)
		},
	}
</script>

Count.vue

<template>
	<div>
		<h1>当前求和为:{{sum}}</h1>
		<h3>当前求和放大10倍为:{{bigSum}}</h3>
		<h3>我在{{school}},学习{{subject}}</h3>
		<h3 style="color:red">Person组件的总人数是:{{personList.length}}</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment(n)">+</button>
		<button @click="decrement(n)">-</button>
		<button @click="incrementOdd(n)">当前求和为奇数再加</button>
		<button @click="incrementWait(n)">等一等再加</button>
	</div>
</template>

<script>
	import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
	export default {
		name:'Count',
		data() {
			return {
				n:1, //用户选择的数字
			}
		},
		computed:{
			//借助mapState生成计算属性,从state中读取数据。(数组写法)
			...mapState('countAbout',['sum','school','subject']),
			...mapState('personAbout',['personList']),
			//借助mapGetters生成计算属性,从getters中读取数据。(数组写法)
			...mapGetters('countAbout',['bigSum'])
		},
		methods: {
			//借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(对象写法)
			...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'}),
			//借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(对象写法)
			...mapActions('countAbout',{incrementOdd:'jiaOdd',incrementWait:'jiaWait'})
		},
		mounted() {
			console.log(this.$store)
		},
	}
</script>

<style lang="css">
	button{
		margin-left: 5px;
	}
</style>

Person.vue

<template>
	<div>
		<h1>人员列表</h1>
		<h3 style="color:red">Count组件求和为:{{sum}}</h3>
		<h3>列表中第一个人的名字是:{{firstPersonName}}</h3>
		<input type="text" placeholder="请输入名字" v-model="name">
		<button @click="add">添加</button>
		<button @click="addWang">添加一个姓王的人</button>
		<button @click="addPersonServer">添加一个人,名字随机</button>
		<ul>
			<li v-for="p in personList" :key="p.id">{{p.name}}</li>
		</ul>
	</div>
</template>

<script>
	import {nanoid} from 'nanoid'
	export default {
		name:'Person',
		data() {
			return {
				name:''
			}
		},
		computed:{
			personList(){
				return this.$store.state.personAbout.personList
			},
			sum(){
				return this.$store.state.countAbout.sum
			},
			firstPersonName(){
				return this.$store.getters['personAbout/firstPersonName']
			}
		},
		methods: {
			add(){
				const personObj = {id:nanoid(),name:this.name}
				this.$store.commit('personAbout/ADD_PERSON',personObj)
				this.name = ''
			},
			addWang(){
				const personObj = {id:nanoid(),name:this.name}
				this.$store.dispatch('personAbout/addPersonWang',personObj)
				this.name = ''
			},
			addPersonServer(){
				this.$store.dispatch('personAbout/addPersonServer')
			}
		},
	}
</script>

Index.js

//该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
import countOptions from './count'
import personOptions from './person'
//应用Vuex插件
Vue.use(Vuex)

//创建并暴露store
export default new Vuex.Store({
	modules:{
		countAbout:countOptions,
		personAbout:personOptions
	}
})

count.js

//求和相关的配置
export default {
	namespaced:true,
	actions:{
		jiaOdd(context,value){
			console.log('actions中的jiaOdd被调用了')
			if(context.state.sum % 2){
				context.commit('JIA',value)
			}
		},
		jiaWait(context,value){
			console.log('actions中的jiaWait被调用了')
			setTimeout(()=>{
				context.commit('JIA',value)
			},500)
		}
	},
	mutations:{
		JIA(state,value){
			console.log('mutations中的JIA被调用了')
			state.sum += value
		},
		JIAN(state,value){
			console.log('mutations中的JIAN被调用了')
			state.sum -= value
		},
	},
	state:{
		sum:0, //当前的和
		school:'尚硅谷',
		subject:'前端',
	},
	getters:{
		bigSum(state){
			return state.sum*10
		}
	},
}

person.js

//人员管理相关的配置
import axios from 'axios'
import { nanoid } from 'nanoid'
export default {
	namespaced:true,
	actions:{
		addPersonWang(context,value){
			if(value.name.indexOf('王') === 0){
				context.commit('ADD_PERSON',value)
			}else{
				alert('添加的人必须姓王!')
			}
		},
		addPersonServer(context){
			axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
				response => {
					context.commit('ADD_PERSON',{id:nanoid(),name:response.data})
				},
				error => {
					alert(error.message)
				}
			)
		}
	},
	mutations:{
		ADD_PERSON(state,value){
			console.log('mutations中的ADD_PERSON被调用了')
			state.personList.unshift(value)
		}
	},
	state:{
		personList:[
			{id:'001',name:'张三'}
		]
	},
	getters:{
		firstPersonName(state){
			return state.personList[0].name
		}
	},
}

结果展示

在这里插入图片描述

本人其他相关文章链接

1.《进阶篇第9章》学习vuex知识点后练习:求和案例_纯vue版代码
2.《进阶篇第9章》学习vuex知识点后练习:把求和案例改成vuex版代码
3.《进阶篇第9章》学习vuex知识点后练习:把求和案例改成getters
4.《进阶篇第9章》学习vuex知识点后练习:把求和案例改成mapState与mapGetters
5.《进阶篇第9章》学习vuex知识点后练习:把求和案例改成mapMutations与mapActions
6.《进阶篇第9章》学习vuex知识点后练习:把求和案例改成多组件共享数据
7.《进阶篇第9章》学习vuex知识点后练习:把求和案例改成vuex模块化编码


http://www.niftyadmin.cn/n/898119.html

相关文章

vue2知识点:理解vuex、安装vuex、搭建vuex环境

文章目录8.1理解 vuex8.1.1vuex 是什么8.1.2什么时候使用 Vuex8.1.3全局事件总线和vuex的区别8.1.5vuex的工作原理图举例&#xff1a;讲解原理图&#xff0c;以求和案例的下拉框选择2&#xff0c;点击后的变化流程讲解8.2安装vuex8.3搭建vuex环境完整代码本人其他相关文章链接8…

vue2知识点:vuex中四个map方法的使用,包括:mapState、mapGetters、mapMutations、mapActions

文章目录8.4四个map方法的使用8.4.1讲解生成代码函数mapState和mapGetters8.4.2讲解生成代码函数mapMutations与mapActions本人其他相关文章链接8.4四个map方法的使用 8.4.1讲解生成代码函数mapState和mapGetters 注意点1&#xff1a; 问题&#xff1a;mapState干啥的&#xf…

vue2知识点:模块化+名命空间

文章目录8.5模块化名命空间本人其他相关文章链接概述&#xff1a;这一块是在学习完vuex知识点及4种map方法之后&#xff0c;进行汇总的【模块化名命空间】&#xff0c;如果前面没学完&#xff0c;看着快会不知所以然。 8.5模块化名命空间 注意点1&#xff1a; 问题&#xff1a;…

《vue2进阶篇:路由》第10章:vue-router,包括基础路由、嵌套路由、路由的query参数和params参数、命名路由、router-link的replace属性、编程式路由、缓存路由组件

文章目录vue-router一、普通html使用“路由”1)手动实现路由案例代码结果展示2 )vue-router举例&#xff1a;使用vue-route实现导航跳转结果展示3)参数路由举例&#xff1a;路由传参并实现打印输出结果展示二、脚手架创建vue项目使用“路由”相关理解10.1.1vue-router的理解10.…

vue2进阶篇:安装路由

命令&#xff1a;npm i vue-router3 注意点1&#xff1a;2022年2月7日以后&#xff0c;vue-router的默认版本&#xff0c;为4版本。 其中vue-router4&#xff0c;只能用在vue3中 而vue-router3&#xff0c;只能用在vue2中。 如果把vue-router4强行安装到vue2中&#xff0c;会报…

vue2进阶篇:vue-router之基础路由

文章目录10.3 基本路由10.3.1 总结: 编写使用路由的 3 步10.3.2案例-基本路由切换效果完整代码本人其他相关文章链接10.3 基本路由 10.3.1 总结: 编写使用路由的 3 步 定义路由组件注册路由使用路由 10.3.2案例-基本路由切换效果 注意点1&#xff1a; 路由组件通常放在pages…

vue2进阶篇:vue-router之嵌套(多级)路由

文章目录10.4嵌套&#xff08;多级&#xff09;路由10.4.1案例-将案例改为“嵌套&#xff08;多级&#xff09;路由”完整代码本人其他相关文章链接10.4嵌套&#xff08;多级&#xff09;路由 10.4.1案例-将案例改为“嵌套&#xff08;多级&#xff09;路由” 注意点1&#xf…

vue2进阶篇:vue-router之路由的query参数

文章目录10.5路由的query参数案例&#xff1a;将案例改为“路由query参数实现方式”完整代码本人其他相关文章链接10.5路由的query参数 注意点1&#xff1a; 问题&#xff1a;跳转路由并携带query参数的2种写法&#xff1f; 答案&#xff1a; 跳转路由并携带query参数&#xff…