首先在store里面定义state及处理函数
import { defineStore } from 'pinia';
import { store } from '/@/store';
export const useAppStore = defineStore({
id: 'app',
state: (): AppState => ({
appLogo:'public/resource/img/logo.png'
}),
actions: {
setAppLogo(newLogo) {
this.appLogo = newLogo;
},
},
})
然后再某个页面使用函数,改变值
import { useAppStore } from '/@/store/modules/app';
const appStore = useAppStore();
// 监听到折叠状态发生变化时执行的操作
watch(
() => getCollapsed.value,
(newCollapsed) => {
if (newCollapsed) {
// 折叠时, 更改全局logo地址
appStore.setAppLogo('public/resource/img/lemon.png');
} else {
appStore.setAppLogo('public/resource/img/logo.png');
}
},
);
再要使用的页面监听使用
import { useAppStore } from '/@/store/modules/app';
const appStore = useAppStore();
const logoImg =ref('')
// 只监听pinia中某一个值的变化
watch(
() => appStore.appLogo,
(newValue) => {
logoImg.value = newValue;
},
);