MarginLon MarginLon
首页
  • Web

    • Web基础
    • Web布局
    • CodeSnippet
  • Vue

    • Vue
  • React

    • React
  • Node.js

    • Node.js
  • 技术文档
  • UI组件库
  • 动画库
  • 图形库
  • 桌面端
  • 学习笔记
  • 生活笔记
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

MarginLon

http://info.cern.ch
首页
  • Web

    • Web基础
    • Web布局
    • CodeSnippet
  • Vue

    • Vue
  • React

    • React
  • Node.js

    • Node.js
  • 技术文档
  • UI组件库
  • 动画库
  • 图形库
  • 桌面端
  • 学习笔记
  • 生活笔记
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Vue

  • Vue-Router

  • Vuex

    • Vuex实例
  • 《Vue》
  • Vuex
MarginLon
2022-10-10

Vuex实例

// main.js
import { createApp } from 'vue';
import App from './App.vue';
import { createStore } from 'vuex';

const store = createStore({
  state () {
    return {
      color: [100, 100, 100]
    };
  },
  mutations:{
    randomColor (state) {
    state.color = [
      Math.floor(Math.random() * 255),
      Math.floor(Math.random() * 255),
      Math.floor(Math.random() * 255)
    ];
  }
  } 
})

const app = createApp(APP);
app.use(store);
app.mount("#app");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!-- App.vue -->
<template>
  <main>
    <button></button>
  </main>
</template>
<script>
  export default {
    computed: {
      bgColor() {
        const colors = this.$store.state.color;
        return `rgb(${colors.join(',')})`;
      }
    },
    methods: {
      changeColor() {
        this.$store.commit('randomColor');
      }
    }
  }
</script>
<style>
  main {
    background-color: v-bind(bgColor);
  }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
编辑 (opens new window)
上次更新: 2023/04/01, 15:26:22
路由元数据

← 路由元数据

最近更新
01
KnockoutJS
11-12
02
综述
10-17
03
前言
10-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 MarginLon | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式