一、组件

  1. <template>
  2. <button @click="handleClick">{{city}}</button>
  3. </template>
  4. <script setup>
  5. import { computed } from '@vue/runtime-core';
  6. import {useStore} from 'vuex'
  7. const store = useStore();
  8. let city = computed(()=>{
  9. return store.state.city
  10. })
  11. function handleClick(){
  12. store.commit("changeCity","苏州")
  13. }
  14. </script>

二、vuex

  1. import { createStore } from 'vuex'
  2. export default createStore({
  3. state: {
  4. city:"武汉"
  5. },
  6. mutations: {
  7. changeCity(state,city){
  8. state.city = city
  9. }
  10. }
  11. })