美文网首页
vue-vuex-action的使用

vue-vuex-action的使用

作者: 望月成三人 | 来源:发表于2020-11-25 23:34 被阅读0次
  • 创建项目选择router和vuex
  • src/route/index.js中
import ActionView from '../views/ActionView.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/state',
    name:"StateView",
    component: StateView
    }
....
  • src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  // data
  state: {
    num: 0,
    age: 18,
    msg: "haha",
    city:""
  },
  //  methods,处理state
  mutations: {
    addNum(state) {
      state.num++
    },
    setNum(state, value) {
      state.num = value
    },
    setCity(state,value) {
      state.city = value
    }
  },
  //  异步方法,如ajax
  actions: {
    setCity1: function(context) {
      let http_url = "https://geoapi.qweather.com/v2/city/lookup?location=chaoyang&key=XXX"
      axios.get(http_url).then((res)=>{
        console.log(res)
        // 这里的context就是store,通过修改mutations里面的方法修改state的值
        context.commit("setCity", res.data.location)
      })
     
    }
    
  },
  modules: {
  }
})
  • src/views/ActionView.vue组件
<template>
    <div class="action">
        <h1>action:</h1>
          <h2>数字:{{num}}</h2>
        <button @click="getCityEvent">获取城市1</button>
        <button @click="setCity1">获取城市2</button>
         <button @click="addNum">新增数字</button>
        <ul>
            <li v-for="(item,index) in city" :key="index">{{item}}</li>
        </ul>
    </div>
</template>
<script>
// 这里可以用map导入sate,action,mutations,直接调用里面的方法
import { mapState,mapActions,mapMutations} from 'vuex'
let mapStateObj = mapState(['city','num'])
let mapActionObj = mapActions(['setCity1'])
let mapMutationsObj = mapMutations(['addNum'])

export default {
    data() {
        return {}
    },
    computed: {
        // 解析state中的参数
        ...mapStateObj,
       
    },
    methods:{
      getCityEvent:function() {
        // dispatch调用action中的方法
        this.$store.dispatch("setCity1")
      },
       // 解析action中的方法
        ...mapActionObj,
       // 解析Mutations中的方法
        ...mapMutationsObj  
    }
}
</script>

相关文章

  • vue-vuex-action的使用

    创建项目选择router和vuex src/route/index.js中 src/store/index.js ...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

  • this的使用

    1.默认绑定,就是函数立即执行。 函数立即执行就是指向window,但是如果是node环境,就是指向全局conso...

  • %in% 的使用

    写在前面:From 生信技能书向量难点之一:%in% 难点 (1)== 与 %in% 的区别== 强调位置,x和对...

网友评论

      本文标题:vue-vuex-action的使用

      本文链接:https://www.haomeiwen.com/subject/oiioiktx.html