前端技术前端开发笔记前端知识

【Vuex 极速指南】

2019-04-04  本文已影响22人  一俢

在这篇文章,你将快速学习到:

如何安装

什么是 Vuex

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

Hello World

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf8">
    <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
    <title>Hello Vuex</title>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
</head>

<body>
    <script>
        var store = new Vuex.Store({
            state: {
                count: 0
            },
            mutations: {
                increment: function(state){
                    state.count++;
                }
            }
        });

        store.commit('increment');
        console.log(store.state.count);
    </script>
</body>

</html>

核心概念

State

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf8">
    <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
    <title>Vuex State</title>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
</head>

<body>
    <div id="app">
        {{ count }}
    </div>
    <script>
        var store = new Vuex.Store({
            state: {
                count: 1
            }
        });
        
        var app = new Vue({
            el: '#app',
            store: store,
            computed: {
                count: function(){
                    return this.$store.state.count
                }
            }
        });
    </script>
</body>

</html>

Getters

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf8">
    <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
    <title>Vuex Getter</title>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
</head>

<body>
    <div id="app">
        {{ doneTodos }}
    </div>
    <script>
        var store = new Vuex.Store({
            state: {
                todos: [
                    {id: 1, text: 'a thing', done: true },
                    {id: 2, text: 'B thing', done: false }
                ]
            },
            getters: {
                doneTodos: function(state){
                    return state.todos.filter(function(todo){
                        return todo.done;
                    });
                }
            }
        });
        
        var app = new Vue({
            el: '#app',
            store: store,
            computed: {
                doneTodos: function(){
                    return this.$store.getters.doneTodos
                }
            }
        });
    </script>
</body>

</html>

Mutations

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf8">
    <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
    <title>Vuex Mutation</title>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
</head>

<body>
    <div id="app">
        {{ count }}
        <button @click="increment">increment</button>
    </div>
    <script>
        var store = new Vuex.Store({
            state: {
                count: 1
            },
            mutations: {
                increment: function(state) {
                    state.count++;
                }
            }
        });
        
        var app = new Vue({
            el: '#app',
            store: store,
            computed: {
                count: function() {
                    return this.$store.state.count
                }
            },
            methods: {
                increment: function() {
                    this.$store.commit('increment');
                }
            }
        });
    </script>
</body>

</html>

Actions

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf8">
    <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
    <title>Vuex Action</title>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
</head>

<body>
    <div id="app">
        {{ count }}
        <button @click="increment">increment</button>
    </div>
    <script>
        var store = new Vuex.Store({
            state: {
                count: 1
            },
            mutations: {
                increment: function(state) {
                    state.count++;
                }
            },
            actions: {
                increment: function(context) {
                    context.commit('increment');
                }
            }
        });
        
        var app = new Vue({
            el: '#app',
            store: store,
            computed: {
                count: function() {
                    return this.$store.state.count
                }
            },
            methods: {
                increment: function() {
                    this.$store.dispatch('increment');
                }
            }
        });
    </script>
</body>

</html>

Modules

〖坚持的一俢〗

上一篇 下一篇

猜你喜欢

热点阅读