首页 > 其他分享 >vue2和vue3的modules :

vue2和vue3的modules :

时间:2022-08-30 14:23:47浏览次数:62  
标签:const modules state vue2 vue3 home data store

store/modules/home.js


export default {
  state: {
    // 服务器数据
    banners: [],
    recommends: []
  },
  mutations: {
    changeBanners(state, banners) {
      state.banners = banners
    },
    changeRecommends(state, recommends) {
      state.recommends = recommends
    }
  },
  actions: {
    fetchHomeMultidataAction(context) {
      return new Promise(async (resolve, reject) => {
        // 3.await/async
        const res = await fetch("http://123.207.32.32:8000/home/multidata")
        const data = await res.json()
        
        // 修改state数据
        context.commit("changeBanners", data.data.banner.list)
        context.commit("changeRecommends", data.data.recommend.list)

        resolve("aaaaa")
      })
    }
  }
}
store/index.js


import { createStore } from 'vuex'

import homeModule from './modules/home'

const store = createStore({
  state: () => ({

  }),
  getters: {
 
  },
  mutations: {
 
  },
  actions: {
  },
  modules: {
    home: homeModule,
  }
})

export default store
使用 : 

<template>
  <div class="home">
    <h2>Home Page</h2>
    <ul>
      <!-- 获取数据: 需要从模块中获取 state.modulename.xxx -->
      <template v-for="item in $store.state.home.banners" :key="item.acm">
        <li>{{ item.title }}</li>
      </template>
      <!-- 2.使用getters时, 是直接getters.xxx -->
    </ul>
  </div>
</template>

<script>
</script>

<script setup>

  import { useStore } from 'vuex'

  // 告诉Vuex发起网络请求
  // 派发事件时, 默认也是不需要跟模块名称
  // 提交mutation时, 默认也是不需要跟模块名称
  const store = useStore()
  store.dispatch("fetchHomeMultidataAction").then(res => {
    console.log("home中的then被回调:", res)
  })

</script>


<style scoped>
</style>

 

标签:const,modules,state,vue2,vue3,home,data,store
From: https://www.cnblogs.com/qd-lbxx/p/16639144.html

相关文章

  • vite+vue2 的学习与问题记录
    描述按照博文[https://juejin.cn/post/6988808776291713060]指导步骤执行完成。问题记录运行npmrundev控制台显示failedtoloadconfigfrom/Users/study-vite-vu......
  • vue3+vuex 的 action 来发送网络请求的
    <template><divclass="app"><h3>HomePage</h3><ul><templatev-for="itemin$store.state.banners":key="item.acm"><li>{{item.title......
  • vue2和vue3的区别?
    vue2和vue3的主要区别在于以下几点:1、生命周期函数钩子不同2、数据双向绑定原理不同3、定义变量和方法不同4、指令和插槽的使用不同5、API类型不同6、是否支持碎片7......
  • vue3+vuex 的 actions 的 使用
    <template><divclass="app">姓名:{{$store.state.nameVuex}}<button@click="btn">基本方法:修改名字</button><br/><button@click="btn1">传递值......
  • vue3 - 封装图表组件
      把相同或者类似的图表进行封装父组件使用:<Report:info="main4":xdata="RXData4":sdata="RSData4":title="title4"......
  • vue3项目-小兔鲜儿笔记-首页03
    1.面板封装提取首页的公用面板进行复用头部标题和副标题由props传入右侧内容由具名插槽right传入查看更多封装成全局组件主体由默认插槽传入......
  • vue3 基础-事件绑定 & 修饰符
    无非就是js的一些事件,按键,鼠标等的一些绑定在vue的实现而已,很好理解.先来看一个基础例子.事件初体验<!DOCTYPEhtml><htmllang="en"><head><title>事......
  • vue3基础入门
    vue3基础入门官方网站:https://v3.vuejs.org/中文文档:https://staging-cn.vuejs.org/guide/introduction.html1、简介1.1、vue是什么?Vue.js(读音/vjuː/,类似于vi......
  • vue3+vuex 的 mutations 的 使用
    <template><divclass="app">姓名:{{$store.state.nameVuex}}<button@click="btn">基本方法:修改名字</button><br/><button@click="btn1">传递值......
  • vue2+vuex 的 mutations 的 使用
    <template><divclass="app">姓名:{{$store.state.nameVuex}}<button@click="btn">基本方法:修改名字</button><br/><button@click="btn1......