# vuex
新建个textStore.js
import { createStore } from "vuex";
import { useMessage } from "naive-ui";
export default createStore({
state: {
test:""
},
mutations: {
setTest(state, text) {
state.test = text
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
页面或其他的js调用
<script lang='ts' setup>
import {ref} from "vue"
import textStore from "../store/textStore"
let teste = ref(textStore.state.test )//直接获取但 但不会双向更新
//这样会test更新了才会变化
const teste = computed(() => {
return textStore.state.test;
});
// 调用mutations的里方法
textStore.commit("setTest", "测试一下");
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14