Vuex实现计数器以及列表展示效果

 更新时间:2021年06月24日 15:30:41   作者:axel10  
这篇文章主要为大家详细介绍了Vuex实现计数器以及列表展示效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本篇教程将以计数器及列表展示两个例子来讲解Vuex的简单用法。

本案例github

从安装到启动初始页面的过程都直接跳过。注意安装时选择需要路由。

首先,src目录下新建store目录及相应文件,结构如下:

index.js文件内容:

import Vue from "vue"
import Vuex from 'vuex'

Vue.use(Vuex);  //务必在new Vuex.Store之前use一下

export default new Vuex.Store({
 state:{
  count:0    //计数器的count
 },
 mutations:{
  increment(state){
   state.count++
  }
 }
})

src下的main.js里注册store

new Vue({
 el: '#app',
 router,
 store,    //注册store
 components: { App },
 template: '<App/>'
});

components文件夹内新建Num.vue组件,内容如下

<template>
 <div>
  <input type="button" value="+" @click="incr" />
  <input type="text" id="input" v-model="count"/>
  <input type="button" value="-"/>
  <br/>
  <router-link to="/list">列表demo</router-link>
 </div>
</template>

<script>
 import store from '../store'
 export default {

  computed:{
   count:{
    get:function () {
     return store.state.count
    },
    set:function (val) {
     store.state.count = val
    }
   }
  },

  methods:{
   incr(){
    // store.commit("increment")
    store.commit("increment")  //触发修改
   }
  }
 }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

router文件夹内配置路由:

import Vue from 'vue'
import Router from 'vue-router'
import Num from '../components/Num'
import List from '../components/List'

Vue.use(Router)

export default new Router({
 routes: [
  {
   path:'/num',
   component:Num
  },

  {
   path:"*",
   redirect:"/num"
  }
 ]
})

完成后启动,即可看到结果。计数器演示完成。

现在开始列表演示。

src目录下新建api文件夹,再新建api文件。

api/cover.js:

const _cover = [
 {"id": 1, "title": "iPad 4 Mini", "price": 500.01, "inventory": 2},
 {"id": 2, "title": "H&M T-Shirt White", "price": 10.99, "inventory": 10},
 {"id": 3, "title": "Charli XCX - Sucker CD", "price": 19.99, "inventory": 5}
];


export default {
 getCover(cb) {
  setTimeout(() => cb(_cover), 100);
/*  $.get("/api/data",function (data) {
   console.log(data);
  })*/

 },
}

修改store/modules/cover.js:(定义数据模型)

import cover from '../../api/cover'

const state = {
 all:[]
};

const getters={
 allCover:state=>state.all  //getter用来提供访问接口
};

const actions = {
 getAllCover({commit}){
  cover.getCover(covers=>{
   commit('setCover',covers)    //触发setCover修改。
  })
 },
 removeCover({commit},cover){
  commit('removeCover',cover)
 }
};

const mutations = {  //mutations用来修改state。
 setCover(state,covers){
  state.all = covers
 },
 removeCover(state,cover){
  console.log(cover.id);
  state.all = state.all.filter(function (OCover) {
   return OCover.id !== cover.id
  })
 }
};

export default {
 state,getters,actions,mutations
}

store内的index.js中注册数据模型:

import Vue from "vue"
import Vuex from 'vuex'
import cover from './modules/cover'

Vue.use(Vuex);  //务必在new Vuex.Store之前use一下

export default new Vuex.Store({

 modules:{
  cover     //注册cover数据模型
 },

 state:{
  count:0    //计数器的count
 },
 mutations:{
  increment(state){
   state.count++
  }
 }
})

components文件夹内新建List.vue组件,内容如下:

<template>
 <div class="list">
  <ul>
   <li v-for="cover in covers" @click="removeCover(cover)">
    {{cover.title}}
   </li>
  </ul>
  <p>
   {{covers}}
  </p>
  <h2>请尝试点击li。</h2>
  <router-link to="/num">计数器demo</router-link>

 </div>
</template>

<script>
import {mapGetters,mapActions} from 'vuex';

export default {
 computed:mapGetters({
  covers:"allCover"   //利用module的getter获得数据
 }),

 methods:mapActions([
  'removeCover'    //利用module的action删除数据
 ]),

 created(){
  this.$store.dispatch('getAllCover')  //调用cover数据模型的getAllCover action 用来初始化列表数据。
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 .list{
  text-align: left;
 }
</style>

路由中注册新组件:

import Vue from 'vue'
import Router from 'vue-router'
import Num from '../components/Num'
import List from '../components/List'

Vue.use(Router)

export default new Router({
 routes: [
  {
   path:'/num',
   component:Num
  },
  {
   path:'/list',
   component:List
  },
  {
   path:"*",
   redirect:"/num"
  }
 ]
})

完成后访问http://localhost:8080/#/list,即可看到结果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue3应用elementPlus table并滚动显示问题

    vue3应用elementPlus table并滚动显示问题

    这篇文章主要介绍了vue3应用elementPlus table并滚动显示问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 一文带你搞懂Vue.js如何实现全选反选功能

    一文带你搞懂Vue.js如何实现全选反选功能

    这篇文章主要为大家详细介绍了Vue.js实现全选反选功能的相关知识,文中是示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下
    2025-01-01
  • 浅谈Vue SPA 首屏加载优化实践

    浅谈Vue SPA 首屏加载优化实践

    本篇文章主要介绍了浅谈Vue SPA 首屏加载优化实践,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Vue3根据动态字段绑定v-model的操作代码

    Vue3根据动态字段绑定v-model的操作代码

    最近在学习vue技术,开发表格的时候,想把表格做成组件,那查询条件就需要动态生成,这就遇到一个问题,vue怎么动态给v-model变量值,本文通过实例代码给大家介绍,对Vue3动态绑定v-model实例代码感兴趣的朋友一起看看吧
    2022-10-10
  • Vue自定义指令使用方法详解

    Vue自定义指令使用方法详解

    这篇文章主要为大家详细介绍了Vue自定义指令的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • vue 动态绑定背景图片的方法

    vue 动态绑定背景图片的方法

    这篇文章主要介绍了vue 动态绑定背景图片的方法,在文末给大家介绍了vue如何给v-for循环的标签添加背景图片,需要的朋友参考下吧
    2018-08-08
  • 关于Nuxt的五种渲染模式的差异和使用场景全解析

    关于Nuxt的五种渲染模式的差异和使用场景全解析

    这篇文章主要介绍了关于Nuxt的五种渲染模式的差异和使用场景全解析,在过去传统开发中,页面渲染任务是由服务端完成的,那么Nuxt是如何渲染的呢,需要的朋友可以参考下
    2023-04-04
  • javaScript与vue获取元素的方法代码示例

    javaScript与vue获取元素的方法代码示例

    在开发中我们可能会遇到这样的问题,文本框聚焦、元素点击等,所以下面这篇文章主要给大家介绍了关于javaScript与vue获取元素的相关资料,需要的朋友可以参考下
    2023-10-10
  • Vue 多层组件嵌套二种实现方式(测试实例)

    Vue 多层组件嵌套二种实现方式(测试实例)

    本篇文章主要介绍了Vue组件嵌套二种实现方式(测试实例),具有一定的参考价值,代码很简单,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • 在Vue中使用Compass的方法

    在Vue中使用Compass的方法

    本篇文章主要介绍了在Vue中使用Compass的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03

最新评论