月球大魔王 發表於 2021-4-15 17:24:00

uni-app中使用vuex

<p>项目中难免都会用到vuex,下面我总结了一下在uni-app中如何使用vuex</p>
<ul>
<li>首先新建文件store/index.js</li>
</ul>
<pre><code class="language-js">import Vue from "vue"
importVuex from "vuex"
Vue.use(Vuex)
export default new Vuex.Store({
      // 全局属性变量
      state:{ // state的作用是定义属性变量。定义一个参数
      num:0,
        price:10,
        name:"苹果",
        testList:[]
      },
})
</code></pre>
<ul>
<li>注:新建的store文件要main.js注册哦</li>
</ul>
<pre><code class="language-js">import store from "./store/index.js"
Vue.prototype.$store=store;
</code></pre>
<p><strong>1、首先是mutations的使用方式</strong></p>
<pre><code class="language-js">//store/index.js文件中
export default new Vuex.Store({
      // 全局属性变量
      state:{ // state的作用是定义属性变量。定义一个参数
      num:0,
        price:10,
        name:"苹果",
        testList:[]
      },
      // 全局同步方法,在methods{this.$store.commit("add")}
      mutations:{
        add(state){
          state.num++;
        },
        down(state){
          state.num--;
        },
      },
})


//页面调用 text/index.vue
&lt;template&gt;
&lt;view&gt;
      &lt;view&gt;vuex测试&lt;/view&gt;
      &lt;view&gt;数量:{{num}}&lt;/view&gt;
        &lt;button type="default" @click="add"&gt;加一&lt;/button&gt;
        &lt;button type="default" @click="down"&gt;减一&lt;/button&gt;
&lt;/vuew&gt;
&lt;/template&gt;
// 页面js方法
computed:{
// 数量
num(){//计算数据,视图随之改变(直接返回num,在页面显示)
    return this.$store.state.num;
},
},
methods: {
add(){// mutations的调用方式,通过commit调用
    this.$store.commit("add")// 直接计算,数据会变化,视图不会更新 ,需要计算属性监听
},
down(){
    this.$store.commit("down")// 直接计算,数据会变化,视图不会更新
},
}
</code></pre>
<p>总结:mutations是一个全局的同步方法,可修改定义在vuex属性的值,在所有页面都可调用,所以有时候在vue中兄弟通讯除了使用事件总线,还可使用vuex的形式,在mutations定义的是数据应该怎样变化。在页面中的调用是通过在方法(methods)中使用this.$store.commit("定义的方法名"),需要注意的是这里的调用用的是commit(actions中使用的是dispatch,两者调用的不同点),如果想使用改变后的值的话,可以在computed中返回的形式使用,如:return this.$store.state.num;<br>
<strong>2、计算属性的应用getters</strong></p>
<pre><code class="language-js">// store/index.js中
//store/index.js文件中
export default new Vuex.Store({
      // 全局属性变量
      state:{ // state的作用是定义属性变量。定义一个参数
      num:0,
        price:10,
        name:"苹果",
        testList:[]
      },
      // 全局同步方法,在methods{this.$store.commit("add")}
      mutations:{
        add(state){
          state.num++;
        },
        down(state){
          state.num--;
        },
      },
      // vuex属性计算,在视图里面当变量使用
      getters:{
        conut(state){
          // 这个函数的执行依赖一个可变的变量
          return state.num*state.price //数据变化返回出新的值
      }
      },
})


//页面调用 text/index.vue
&lt;template&gt;
&lt;view&gt;
      &lt;view&gt;vuex测试&lt;/view&gt;
      &lt;view&gt;数量:{{num}}&lt;/view&gt;
      &lt;view&gt;商品名:{{name}}&lt;/view&gt;
      &lt;view&gt;总价:{{conut}}&lt;/view&gt;
        &lt;button type="default" @click="add"&gt;加一&lt;/button&gt;
        &lt;button type="default" @click="down"&gt;减一&lt;/button&gt;
&lt;/vuew&gt;
&lt;/template&gt;
// 页面js方法
computed:{
// 数量
num(){//计算数据,视图随之改变(直接返回num,在页面显示)
    return this.$store.state.num;
},
// 价格
conut(){// 仅在这里调用即可
   return this.$store.getters.conut;
},
},
methods: {
add(){
    this.$store.commit("add")// 直接计算,数据会变化,视图不会更新 ,需要计算属性监听
},
down(){
    this.$store.commit("down")// 直接计算,数据会变化,视图不会更新
},
}
</code></pre>
<p>总结:getters是一个计算属性,可以计算vuex内的数据,调用方式是在页面的computed中通过this.$store.getters.conut;(conut为定义的方法名)可直接返回,返回出的函数名可只用用于页面显示<br>
<strong>3、actions异步请求的使用</strong></p>
<pre><code class="language-js">// store/index.js中
//store/index.js文件中
export default new Vuex.Store({
      // 全局属性变量
      state:{ // state的作用是定义属性变量。定义一个参数
      num:0,
        price:10,
        name:"苹果",
        testList:[]
      },
      /*
      // 全局同步方法,在methods{this.$store.commit("add")}
      mutations:{
        add(state){
          state.num++;
        },
        down(state){
          state.num--;
        },
      },
      // vuex属性计算,在视图里面当变量使用
      getters:{
        conut(state){
          // 这个函数的执行依赖一个可变的变量
          return state.num*state.price //数据变化返回出新的值
      }
      },
      */
      actions:{
      testActions(context){
          // context包含了state,mutations,getters
          // console.log(context)
          // 执行异步参数,通用ajax
          setTimeout(()=&gt;{// 模拟异步请求
            context.state.testList=["猪猪侠","超人强","小飞飞","猪博士","喜羊羊"]
          },2000)
      }
      }
})


//页面调用 text/index.vue
&lt;template&gt;
&lt;view&gt;
      &lt;view&gt;vuex测试&lt;/view&gt;
      &lt;view&gt;数量:{{num}}&lt;/view&gt;
      &lt;view&gt;商品名:{{name}}&lt;/view&gt;
      &lt;view&gt;总价:{{conut}}&lt;/view&gt;
        &lt;button type="default" @click="add"&gt;加一&lt;/button&gt;
        &lt;button type="default" @click="down"&gt;减一&lt;/button&gt;
      &lt;button type="default" @click="testActions"&gt;testActions&lt;/button&gt;
&lt;/vuew&gt;
&lt;/template&gt;
// 页面js方法
computed:{
// 数量
num(){//计算数据,视图随之改变(直接返回num,在页面显示)
    return this.$store.state.num;
},
// 价格
conut(){// 仅在这里调用即可
   return this.$store.getters.conut;
},
// 计算vuex中actions
testList(){
    console.log(this.$store.state.testList) // 会打印得到异步请求的参数
    return this.$store.state.testList;    // 返回可直接使用,如遍历数据
}
},
methods: {
add(){
    this.$store.commit("add")// 直接计算,数据会变化,视图不会更新 ,需要计算属性监听
},
down(){
    this.$store.commit("down")// 直接计算,数据会变化,视图不会更新
},
// vuex中调用actions方法
testActions(){   // 点击按钮执行函数,触发actions里面的方法,在computed计算属性中可获得这个值
    this.$store.dispatch("testActions") // 触发actions里面的方法
}
}
</code></pre>
<p>总结:actions是vuex中异步请求的方法,一般用在公共请求数据接口的地方,调用方式是this.$store.dispatch("方法名")(注:这里是dispatch,与mutations中使用的commit的方式不同,)</p><br><br>
来源:https://www.cnblogs.com/axingya/p/14663499.html
頁: [1]
查看完整版本: uni-app中使用vuex