• Vuex 样例1


    批注 2020-04-23 144244

    app.js

    /**
     * First we will load all of this project's JavaScript dependencies which
     * includes Vue and other libraries. It is a great starting point when
     * building robust, powerful web applications using Vue and Laravel.
     */
    
    require('./bootstrap');
    
    window.Vue = require('vue');
    
    import store from './Vuex/index';
    /**
     * The following block of code may be used to automatically register your
     * Vue components. It will recursively scan this directory for the Vue
     * components and automatically register them with their "basename".
     *
     * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
     */
    
    // const files = require.context('./', true, /.vue$/i)
    // files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))
    
    Vue.component('example-component', require('./components/ExampleComponent.vue').default);
    Vue.component('store-component', require('./components/StoreComponent.vue').default);
    /**
     * Next, we will create a fresh Vue application instance and attach it to
     * the page. Then, you may begin adding components to this application
     * or customize the JavaScript scaffolding to fit your unique needs.
     */
    
    const app = new Vue({
        el: '#app',
        store,
    });
    
    

    Vuex/index.js:

    import Vue from 'vue';
    import 'es6-promise/auto'
    import Vuex from 'vuex';
    
    Vue.use(Vuex);
    
    const store = new Vuex.Store({
        state: {
            count: 0,
        },
        mutations: {
            increment(state) {
                state.count++;
            },
            decrement(state) {
                state.count--;
            },
        },
    });
    export default store;
    
    

    web.php:

    <?php
    
    use IlluminateSupportFacadesRoute;
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
    
    Route::get('/', function () {
        return view('welcome');
    });
    
    Auth::routes();
    
    Route::get('/home', 'HomeController@index')->name('home');
    Route::get('/store-test', function (Request $request) {
        return view('store');
    });
    
    

    store.blade.php:

    @extends('layouts.app')
    
    @section('content')
        <store-component></store-component>
    @endsection
    
    

    StoreComponent.vue:

    <template>
        <div>
            <button @click="increment"> {{count}}</button>
            <button @click="decrement"> {{count}}</button>
        </div>
    </template>
    
    <script>
        export default {
            name: "StoreComponent",
            computed: {
                count() {
                    return this.$store.state.count;
                }
            },
            methods: {
                increment() {
                    this.$store.commit('increment');
                },
                decrement() {
                    this.$store.commit('decrement');
                },
            }
        }
    </script>
    
    <style scoped>
    
    </style>
    
    

    访问:

    http://vuex.test/store-test

    效果:

    vue.test vuex1

  • 相关阅读:
    curl获取HTTP返回状态码
    存储过程中如何实现从数组获取数据
    ElasticsearchParseException: malformed, expected settings to start with 'object', instead was [VALUE_STRING]
    【并发编程】如果让你用三个线程循环打印ABC,你有几种写法?
    【基础】IdentityHashMap
    【基础】ThreadPoolExecutor
    【算法】快速排序
    【Java8新特性Stream】list转map
    【算法】华为南研所-括号匹配
    windows sourceTree 密码错误
  • 原文地址:https://www.cnblogs.com/dzkjz/p/12760734.html
Copyright © 2020-2023  润新知