继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Vuex探索与学习总结---get-started

resharpe
关注TA
已关注
手记 102
粉丝 7244
获赞 3476
Vuex探索与学习总结---get-started
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>vue-x</title>
    <script src="../vue.js"></script>
    <script src="../vue-router.js"></script>
    <script src="../vuex.js"></script>
</head>
<body>
<h4>What is Vuex?</h4>
<p>Vuex is a state management pattern + library for Vue.js applications.</p>
<div class="app">
    <p>{{ count }}</p>
    <p>
        <button @click="increment">+</button>
        <button @click="decrement">-</button>
    </p>
</div>
<script>
    const store = new Vuex.Store({
        state: {
            count: 0
        },
        mutations: {
            increment: state => state.count++,
            decrement: state => state.count--
        }
    })

    const app = new Vue({
        el: '.app',
        computed: {
            count () {
                return store.state.count
            }
        },
        methods: {
            increment () {
                store.commit('increment')
            },
            decrement () {
                store.commit('decrement')
            }
        }
    })
</script>
<div id="app"></div>
<script>
    const Counter = {
        template: `<div>{{ count }}</div>`,
        computed: {
            count () {
                return this.$store.state.count
            }
        }
    };
    const app = new Vue({
        el: '#app',
        // 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
        store,
        components: { Counter },
        template: `
    <div class="app">
      <counter></counter>
    </div>
  `
    })
</script>

</body>

</html>
打开App,阅读手记
4人推荐
发表评论
随时随地看视频慕课网APP