首页 > 其他分享 >假期vue学习笔记15 求和mapstate_mapgetter

假期vue学习笔记15 求和mapstate_mapgetter

时间:2024-02-28 17:57:41浏览次数:19  
标签:vue 15 name sum value mapstate state import store

 

import Vue from 'vue'
import App from './App.vue'
import store from './store'
Vue.config.productionTip = false

new Vue({     el:'#root',     render: h => h(App),     store,     beforeCreate(){         Vue.prototype.$bus = new Vue();     } })     <template>         <div >             <Count/>             <Person/>         </div> </template>
<script>
import Count from './components/Count.vue' import Person from './components/Person.vue' export default{     name:'App',     components:{         Count,         Person     },     data() {         return {                 }     }, } </script>

<style lang="css">       </style>     //该文件用于创建Vuex中最为核心的store import Vue from 'vue' import Vuex from 'vuex'
Vue.use(Vuex) //准备actions——用于响应组件中的动作 const actions = {     jia(context,value){         context.commit('JIA',value)     },     jian(context,value){         console.log('方法被调用了')         context.commit('JIAN',value)     } } //准备mutations——用于操作数据(store) const mutations = {     JIA(state,value){         state.sum += value     },     JIAN(state,value){         state.sum -= value     },     ADD_PERSONS(state,value){         state.personList.unshift(value)     } } //准备state——用于存储数据 const state = {     sum:0, //当前的和     school:'尚硅谷',     subject:'数学',     personList:[         {id:'001',name:'张三'}     ] } //用于将state中的数据进行加工 const getters = {     bigSum(state){       return  state.sum*10     } }

export default new Vuex.Store({     actions,     mutations,     state,     getters })   <template>     <div>         <h1>人员列表</h1>         <input type="text" placeholder="请输入名字" v-model="name">         <button @click="add">添加</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.personList             }         },         methods:{             add(){                 const personObj = {id:nanoid(),name:this.name}                 this.$store.commit('ADD_PERSONS',personObj)                 this.name=''             }         }     } </script>

<style>

</style>     <template>     <div>         <h1>当前求和为:{{sum}}</h1>         <h1>十倍的和为:{{bigSum}}</h1>         <h1>{{xuexiao}}</h1>         <h1>{{xueke}}</h1>         <h3>下方总人数为:{{$store.state.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">当前求和为基数再加</button>         <button  @click="incrementWait">等一等再加</button>     </div> </template>
<script> import {mapState,mapGetters,mapMutations} from "vuex";     export default{         name:'Count',         data() {             return {                 n:1, //用户选择的数字             }         },         methods:{
            ...mapMutations({increment:'JIA',decrement:'JIAN',personList:'personList'}),
            incrementOdd(){                 if(this.$store.state.sum % 2){                     this.$store.dispatch('jia',this.n)                 }                             },
            incrementWait(){                 setTimeout(()=>{                     this.$store.dispatch('jia',this.n)                 },500)             }         },         computed:{             //借助mapstate生成计算属性。从state中读取数据。(对象写法)             // ...mapState({he:'sum',xuexiao:'school',xueke:'subject'})             //数组写法             ...mapState(['sum','school','subject']),
            ...mapGetters({bigSum:'bigSum'})         }
    } </script>
<style>     button{         margin-left: 5px;     } </style>

标签:vue,15,name,sum,value,mapstate,state,import,store
From: https://www.cnblogs.com/hbro/p/18041269

相关文章

  • 假期vue学习笔记16 vuex多组数据共享
     <template>  <div>    <h1>当前求和为:{{sum}}</h1>    <h1>十倍的和为:{{bigSum}}</h1>    <h1>{{xuexiao}}</h1>    <h1>{{xueke}}</h1>    <h3>下方总人数为:{{$store.state.personList......
  • 假期vue学习笔记07 todo事件的本地存储
     用本地存储改写前面的todo案例 <template>    <li>      <label>        <inputtype="checkbox":checked="todo.done"@change="handleCheck(todo.id)"/>        <spanv-show="!tod......
  • 假期vue学习笔记08 绑定和解绑
     <template>  <divclass="app">    <h1>{{msg}}</h1>    <!--props子给父传递事件-->    <School:getSchoolName="getSchoolName"/>    <!--通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@过v-......
  • 假期vue学习笔记09 全局事件总栈
     <template>  <div class="school">    <h2>学校名称:{{name}}</h2>     <h2>学校地址:{{address}}</h2>  </div></template><script>  exportdefault{    name:'School'......
  • 假期vue学习笔记10 pubsub
     <template>  <divclass="app">    <h1>{{msg}}</h1>    <School/>    <Student/>  </div></template><script>importStudentfrom'./components/Student.vue'imp......
  • 假期vue学习笔记11 动画
    <template>  <divid="root">    <Test/>    <Test2/>    <Test3/>  </div></template><script>importTestfrom'./components/Test.vue'importTest2from'./comp......
  • 架构漫谈——1500字
    架构漫谈:首先是什么是架构,读完之后我自己的对架构的理解就是一种为了方便人们解决问题的一种方案,具体是怎么方便解决问题的呢? 总结下来:先对问题进行分析,再对问题进行切分,由不同的人进行不同的工作,然后使这些部分有机的结合为一个整体,这就是架构,是一个方便解决问题的过程。接......
  • 假期vue学习笔记01 ref
    <template>  <div>    <h1v-text="msg"ref="title"></h1>    <button@click="showDOM">点我输出上方的DOM元素</button>    <School/>  </div></template><script......
  • 假期vue学习笔记02 mixin
    <template>  <div >    <h2@click="showName">学校名称:{{name}}</h2>     <h2>学校地址:{{address}}</h2>  </div></template><script>  exportdefault{    name:'School'......
  • javaweb02-JavaScript&vue
    JavaScript控制网页行为js引入方式内部脚本:script标签外部脚本:js文件js基础语法书写语法区分大小写每行结尾分号可有可无,建议写上输出语句警告框window.alerthtml输出document.write浏览器控制台console.log变量用var关键字声明变量JavaScript是一......