[Efficient] Extremely simplified vuex.js (only 6 lines of code), making rapid and agile development not a dream!

 


vuex.js 

import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex);
// 设置vuex所有变量
let state = {
    _qiangGe: false,//需要添加全局变量就在此处回车添加一个即可,非常方便!!!
}, getters = {}, mutations = {}, actions = {};
Object.keys(state).forEach(k => getters[k] = s => s[k]);
Object.keys(state).forEach(k => mutations[k] = (s, v) => s[k] = v);
Object.keys(state).forEach(k => actions[k] = ({ commit }, v) => commit(k, v));
export default new Vuex.Store({ state, getters, mutations, actions });

application code

<template>
    <div>
        <el-input v-model="value" placeholder="请输入内容" clearable></el-input>
        <br><br>
        <el-button type="primary" @click="$store.dispatch('_qiangGe', value);">修改vuex中的_qiangGe为输入框中的内容</el-button>
        <br><br>
        <div>显示vuex中的_qiangGe此时的值:{
   
   { $store.getters._qiangGe }}</div>
    </div>
</template>
    
<script>
export default { data() { return { value: '', } }, }; 
</script> 

 Getting started articles only takes three minutes! Just create a vuex.js file and let you learn to use Vuex right away, even though Vuex is a tasteless one! (Throw away the store folder and the js files such as index, getters, actions, mutations, etc. in it!)_Your beloved strong brother's blog-CSDN blog Preliminary reminder: One day, I want to achieve an effect → click a button to change a global variable, and let all positions bound to the variable render asynchronously. I tried to use a global js file to store the variable. Although the value is the variable, it has not been rendered asynchronously. Then I use the window. global variable method, Nima! I haven't opened the page yet and reported an error of global variable of undefined. I just remembered that the window global object is empty before the page is loaded! What can I do with this? So I thought about a thing called Vuex, so I fiddled with it and shared it with everyone! I read the official documents, and also read a lot of forum blogs, and found that everyone created a sotre._vuex.js under src https://blog.csdn.net/qq_37860634/article/details/119792336

Guess you like

Origin blog.csdn.net/qq_37860634/article/details/131741966