import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); const store = new Vuex.Store({ state: { name: "Jack Chan", age: 18, }, mutations: { //接受页面参数,改变state中数据 }, getters: { age: function (state) { return state.age;//返回state数据中的年龄。 } }, actions: {} }); export default store
<template> <div> {{age}} </div> </template> <script> import store from '../../../utils/store'; export default { components: { }, data () { return {} }, computed: { age: function () { return store.getters.age; } }, } </script> <style scoped> </style>