VueX了解

10 篇文章 0 订阅
4 篇文章 0 订阅

 

1、 安装vuex

npm install vuex -S

npm install xxx -S,也就是 npm install module_name --save 写入dependencies(生产环境)
npm install xxx -D,也就是 npm install module_name --save-dev 写入devDependencies(开发环境)

2、创建store目录

 state.js

//前端数据库
export default {



}

mutation.js

//改变sate中的值(同步)
export default {
  methodName: (state, payload) => {
    state.属性名 = payload.属性名;
  }
}

action.js

export default{



}

getters,js

export default {
  methodName: (state) => {
    return state.属性名;
  }
}

index.js

import Vue from "vue"
import Vuex from "vuex"
import actions from "@/store/actions"
import getters from "@/store/getters"
import mutations from "@/store/mutations"
import state from "@/store/state"
Vue.use(Vuex)
const store=new Vuex.Store({state,getters,actions,mutations})
export default store

3、main.js引入store

import store from './store/index'

 并且在vue实例中添加store

4、在组件中使用

首先需要在store中定义出属性,再定义mutation中的方法,用以设置store中的属性,再getters中定义得到属性值得方法。

跨组件传值:

数据表格修改

this.$store.commit('setRowChance', {
            rowChance: row
});

可以将数据表格中的row作为整体传递过来(scope.row)

然后在目标组件

this.form = this.$store.getters.getRowChance;

这里的form只需要将属性名与数据表格中的列名一致就可以接受到全部参数了。

进阶

vuex在刷新时会将其中的数据进行刷新,所以一些放在vuex中的数据会在刷新网页值丢失,这个时候就需要将vuex中的值放入sessionStorage里。

只需要在App.vue中添加代码:

  created:function(){
     //在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem("store") ) {
     this.$store.replaceState(Object.assign({},this.$store.state,JSON.parse(sessionStorage.getItem("store"))))     
    } 
    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload",()=>{
      sessionStorage.setItem("store",JSON.stringify(this.$store.state))
    })
    
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vuex 是一个专门为 Vue.js 应用程序开发的状态管理库。它允许您在应用程序中集中管理状态,并且使得状态的变化可追踪、可调试和可维护。以下是一个简单的 Vuex 教程,帮助您开始使用 Vuex。 1. 安装 Vuex 您可以使用 npm 或 yarn 安装 Vuex,命令如下: ```bash npm install vuex ``` 或者 ```bash yarn add vuex ``` 2. 创建 Vuex Store 在您的 Vue.js 应用程序中,您需要先创建一个 Vuex Store。Store 是一个容器,它保存了应用程序中所有的状态(也称为 store)。在该容器中,您可以定义状态、修改状态以及监听状态的变化。 ```javascript import Vuex from 'vuex' import Vue from 'vue' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++ } }, actions: { increment(context) { context.commit('increment') } } }) export default store ``` 上面的代码中,我们定义了一个名为 `store` 的 Vuex Store。在 `state` 对象中,我们定义了一个名为 `count` 的状态。在 `mutations` 对象中,我们定义了一个名为 `increment` 的 mutation,该 mutation 用于修改 `count` 状态。在 `actions` 对象中,我们定义了一个名为 `increment` 的 action,该 action 用于调用 `increment` mutation。 3. 在 Vue.js 应用程序中使用 Vuex Store 您可以将 Vuex Store 注入到 Vue.js 应用程序中,以便在组件中使用。 ```javascript import Vue from 'vue' import App from './App.vue' import store from './store' new Vue({ el: '#app', store, render: h => h(App) }) ``` 在组件中,您可以使用 `mapState` 辅助函数来获取状态,并使用 `mapMutations` 辅助函数来提交 mutation。 ```javascript import { mapState, mapMutations } from 'vuex' export default { computed: { ...mapState(['count']) }, methods: { ...mapMutations(['increment']) } } ``` 4. 修改状态 在组件中,您可以使用 `this.$store.commit` 方法来提交 mutation,从而修改状态。 ```javascript methods: { increment() { this.$store.commit('increment') } } ``` 在 action 中,您可以使用 `context.commit` 方法来提交 mutation,从而修改状态。 ```javascript actions: { increment(context) { context.commit('increment') } } ``` 5. 使用 getter getter 可以对状态进行计算,从而派生出一个新的状态。在 Vuex Store 中,您可以使用 `getters` 对象定义 getter。 ```javascript const store = new Vuex.Store({ state: { count: 0, doubleCount: 0 }, mutations: { increment(state) { state.count++ } }, actions: { increment(context) { context.commit('increment') } }, getters: { doubleCount(state) { return state.count * 2 } } }) ``` 在组件中,您可以使用 `mapGetters` 辅助函数来获取 getter。 ```javascript import { mapGetters } from 'vuex' export default { computed: { ...mapGetters(['doubleCount']) } } ``` 这就是一个简单的 Vuex 教程。它可以帮助您开始使用 Vuex,但 Vuex 的功能远不止于此。如果您想深入了解 Vuex,请参阅 Vuex 官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无感_K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值