zoukankan      html  css  js  c++  java
  • vuex 获取数据

    npm i vuex --save

    2: store文件夹下 index.js
    import Vue from 'vue'
    import Vuex from 'vuex'

    Vue.use(Vuex)

    export const store = new Vuex.Store({
    state: {
    login: 'login',
    test: 'test'
    }
    })

    3: main.js

    import Vue from 'vue'
    import Vuex from 'vuex'
    import App from './App'
    import {store} from './store'

    Vue.use(Vuex)

    new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
    })

    获取vuex中的数据一般写在computed(计算)中

    import {mapState, mapGetters, mapMutations} from 'vuex'
    export default {
    data () {
    return {
    }
    },
    computed: {
    ...mapState(['user']),
    ...mapGetters(['person'])
    },
    methods: {
    // ...mapMutations(['setUser']), //调用mutations中方法的第一种方式
    submitUserNameForm () {
    // this.setUser('456')
    this.$store.commit('setUser','456') //调用mutations中方法的第二种方式
    }
    }
    }

    详情参看https://blog.csdn.net/a3060858469/article/details/80717824

  • 相关阅读:
    node.js入门
    分布式爬虫
    ES6入门
    Vue.js入门
    用scrapy爬取亚马逊网站项目
    垃圾回收器
    HTTP协议详解
    有效的邮箱地址
    C#中正则表达式的使用
    抽象类
  • 原文地址:https://www.cnblogs.com/cs122/p/9782904.html
Copyright © 2011-2022 走看看