用actions改变store中的值,在store中打印值也发生了变化,但是watch就是监听不到这个变化 监听的字段是整型,其它的值变化都能监听到,但是刷新页面值从0变为3就监听不到
时间: 2024-02-02 16:03:46 浏览: 30
这个问题可能是由于你的 watch 监听的字段没有正确设置导致的。在 Vue.js 中,当你使用 `watch` 监听一个对象的属性时,只有当这个属性被赋予新值时才会触发监听器。如果你只是像这样改变一个属性的值:
```javascript
this.$store.state.count = 3;
```
那么 `watch` 是无法监听到这个变化的,因为这个属性并没有被赋予新值。正确的做法是使用 Vuex 提供的更新 state 的方法来改变属性的值,例如:
```javascript
this.$store.commit('updateCount', 3);
```
这样就能触发 `watch` 监听器了。另外,如果你使用的是 Vue.js 2.x 版本,还需要注意一下 `watch` 的用法,具体可以参考 Vue.js 的官方文档。
相关问题
vue3 script里写setup 模式下 用pinia全局管理在兄弟页面存值 另一个兄弟页面监听值得变化
在Vue 3中,你可以使用Pinia全局状态管理库来在兄弟页面之间共享和监听数据的变化。以下是一个示例:
首先,在你的`main.js`文件中,创建并注册Pinia实例:
```javascript
// main.js
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';
const app = createApp(App);
const pinia = createPinia();
app.use(pinia);
app.mount('#app');
```
接下来,创建一个全局的Pinia store来管理数据:
```javascript
// store.js
import { defineStore } from 'pinia';
export const useGlobalStore = defineStore('global', {
state: () => ({
sharedValue: '',
}),
actions: {
setSharedValue(newValue) {
this.sharedValue = newValue;
},
},
});
```
在上面的示例中,我们定义了一个名为`global`的store,其中包含了一个名为`sharedValue`的状态,并且提供了一个名为`setSharedValue`的action来更新该状态的值。
接下来,在兄弟页面中使用该全局store:
```javascript
// BrotherA.vue
<template>
<div>
<input v-model="inputValue" placeholder="Enter a value" />
<button @click="updateSharedValue">Update Value</button>
</div>
</template>
<script>
import { defineComponent } from 'vue';
import { useGlobalStore } from './store';
export default defineComponent({
name: 'BrotherA',
setup() {
const globalStore = useGlobalStore();
const inputValue = '';
const updateSharedValue = () => {
globalStore.setSharedValue(inputValue);
};
return {
inputValue,
updateSharedValue,
};
},
});
</script>
```
在上面的示例中,我们通过`useGlobalStore`函数来获取全局store的实例,并使用`setSharedValue` action来更新`sharedValue`的值。
最后,在另一个兄弟页面中监听`sharedValue`的变化:
```javascript
// BrotherB.vue
<template>
<div>
<p>{{ sharedValue }}</p>
</div>
</template>
<script>
import { defineComponent, watch } from 'vue';
import { useGlobalStore } from './store';
export default defineComponent({
name: 'BrotherB',
setup() {
const globalStore = useGlobalStore();
const sharedValue = globalStore.sharedValue;
watch(
() => globalStore.sharedValue,
(newValue, oldValue) => {
console.log('sharedValue changed:', newValue);
}
);
return {
sharedValue,
};
},
});
</script>
```
在上面的示例中,我们使用`watch`函数来监听`globalStore.sharedValue`的变化,并在发生变化时执行相应的操作。
通过以上设置,你可以在Vue 3中使用Pinia全局状态管理库,在兄弟页面之间共享和监听数据的变化。
this.$store.dispatch传递的参数怎么在actions中的方法使用
在actions中的方法可以通过两种方式来获取this.$store.dispatch传递的参数。
方法一:直接在actions方法中使用参数
```javascript
// 在actions中定义方法
actions: {
changetest(context, payload) {
// 使用payload参数
console.log(payload); // 输出传递的参数
}
}
```
方法二:使用ES6的解构赋值语法
```javascript
// 在actions中定义方法
actions: {
changetest({ commit }, { payload }) {
// 使用payload参数
console.log(payload); // 输出传递的参数
}
}
```
以上两种方法都可以在actions中获取到this.$store.dispatch传递的参数。第一种方法直接使用context参数来获取,第二种方法使用ES6的解构赋值语法来获取。在方法中可以根据需要对参数进行处理或者调用其他方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)