zoukankan      html  css  js  c++  java
  • vuex之仓库数据的设置与获取

    如果你之前使用过vue.js,你一定知道在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,那么接下来我们就来学习一下vuex是如何修改状态值的。

    1. 搭建Demo

    使用vue create 项目名创建一个项目,在src根目录下创建一个components目录并在该目录下面创建2个组件:header.vue,content.vue,在App.vue根组件中进行引入然后展示,删除不必要的组件。

    <template>
      <div style="height:60px;" class="header">
        <span></span>
        <span></span>
      </div>
    </template>
    
    <script>
    export default {
      
    }
    </script>
    
    <style>
    .header span{
        display: inline-block;
         32px;
        height: 32px;
        cursor: pointer;
      }
    .header span:first-child{
      background: red;
    }
    
    .header span:last-child {
      background: blue;
    }
    
    </style>
    header.vue
    <template>
      <div class="content" :style="`background: pink`">
        Content
      </div>
    </template>
    
    <script>
    export default {
      mounted(){
        
      }
    }
    </script>
    
    <style>
      .content {
        height: 600px;
        text-align: center;
        line-height: 600px;
      }
    </style>
    content.vue
    <template>
      <div id="app">
        <Header></Header>
        <Content></Content>
      </div>
    </template>
    
    <script>
    import Header from "./components/header";
    import Content from "./components/content";
    export default {
      // 注册组件
      components: {
        Header,
        Content
      }
    };
    </script>
    
    <style>
    </style>
    App.vue

    页面效果:

    下面需要实现点击红色按钮使得背景变为红色,点击蓝色按钮使得背景变成蓝色,下面使用vuex实现。

    2. 设置和获取仓库中的数据

     先安装vuex。

    yarn add vuex

    在src根目录下创建store,然后创建一个index.js文件。

    import Vuex from "vuex";
    import Vue from "vue";
    
    // 注册插件
    Vue.use(Vuex);
    
    // 创建仓库实例
    const store = new Vuex.Store({
      // 仓库的数据
      state: {
        test_data: "this is some test data",
        color: "light-green"
      },
    
      // 同步修改state的值
      mutations: {
        // mutations下的方法第一个参数是固定state
        // 第二个参数是传进来的参数
        setColor(state, color) {
          state.color = color;
        }
      }
    });
    
    export default store;
    stroe/index.js

    实现后的代码

    <template>
      <div style="height:60px;" class="header">
        <span @click="handleClick('red')"></span>
        <span @click="handleClick('blue')"></span>
      </div>
    </template>
    
    <script>
    export default {
      methods: {
        handleClick(color){
          // 不推荐使用这个方法来修改state的值
          // this.$store.state.color = color;
          
          // this.$store.commit调用mutations下面的方法
          // 第一个参数就是mutations下面的的具体方法
          // 第二个参数就是传递给方法的参数
          this.$store.commit("setColor", color)
        }
      }
    }
    </script>
    
    <style>
    .header span{
        display: inline-block;
         32px;
        height: 32px;
        cursor: pointer;
      }
    .header span:first-child{
      background: red;
    }
    
    .header span:last-child {
      background: blue;
    }
    
    </style>
    header.vue
    <template>
      <div class="content" :style="`background: ${$store.state.color}`">Content</div>
    </template>
    
    <script>
    export default {
      mounted() {
        // this.$store当把仓库引入到main.js的时候,组件可以通过this.$stroe获取仓库的数据
        console.log(this.$store);
      }
    };
    </script>
    
    <style>
    .content {
      height: 600px;
      text-align: center;
      line-height: 600px;
    }
    </style>
    content.vue
    import Vue from 'vue'
    import App from './App.vue'
    import store from './store'
    
    Vue.config.productionTip = false
    
    new Vue({
      render: h => h(App),
      store,
    }).$mount('#app')
    main.js
  • 相关阅读:
    Create data packages for the data import in Dynamics 365 Finance and Operations from Invoice PDFs
    Tutorial: Create an ASP.NET Core Blazor WebAssembly app using Microsoft Dataverse
    Tutorial: Register an app with Azure Active Directory
    WPS JSA 宏编程(JS):7.用户窗体
    OSCP Security Technology
    OSCP Security Technology
    select * 和select 1 以及 select count(*) 和select count(1)的区别
    你只会用 map.put?试试 Java 8 compute ,操作 Map 更轻松!
    SpringBoot+Vue实现第三方QQ登录(二)
    SpringBoot+Vue实现第三方QQ登录(一)
  • 原文地址:https://www.cnblogs.com/replaceroot/p/11171517.html
Copyright © 2011-2022 走看看