如何将输入事件传递给 Vuex 的 getter 存储?

huangapple go评论49阅读模式
英文:

How to pass an input event to the getter store Vuex?

问题

如何将输入字段传递给 getter?

我正在尝试将输入字段作为参数传递给 getter。如何将参数传递给 getter?

<input
      type="text"
      placeholder="Search here"
      v-model="search"
      :input=event.target.value"
    />


export const mutations = {
  setProducts(state, data) {
    state.products = data;
  },

export const actions = {
  async getProducts(context) {
    const res = await fetch(...

      context.commit('setProducts', result);
 
  },
}
英文:

How to pass an input field to the getter?

I'm trying to pass th einput field to the getter as a paramether. How to pass the param to the getter ?

<input
      type="text"
      placeholder="Search here"
      v-model="search"
      :input=event.target.value"
    />


export const mutations = {
  setProducts(state, data) {
    state.products = data;
  },

export const actions = {
  async getProducts(context) {
    const res = await fetch(...

      context.commit('setProducts', result);
 
  },
}

答案1

得分: 1

Gettersvuex store 的基本 computed 属性。它们将 state 作为第一个参数,并将其他 getters 作为第二个参数。如果你想要将某些东西作为参数发送到 store,然后对其执行操作,你需要在 store 中定义一个 action,然后在你的组件中调用该 action 并传递所需的数据给它。Getters 用于访问你的 vuex state 并将它们返回给你的组件。

示例 getter:
store.js

    // 初始化 store
    state: {
      todos: []
    },
    actions: {
      getTodos(/*来自组件的数据*/) {
        // 将数据设置到 todos 状态并在组件中调用此 action
        state.todos = //来自组件的数据
     }
    },
    getters: {
      todos(state) {
        return state.todos;
      }
    }

编辑:

以下是你的 component 文件的示例。

component.vue

    <input
      type="text"
      placeholder="在此搜索"
      v-model="search"
     />

    <script>
    import { ref } from 'vue';
    import { getProducts } from '../store';

    const search = ref('');

    const products = () => {
        getProducts(search.value);
    }
    </script>

store.js

    export const state = () => ({
      products: [],
    });
    
    export const mutations = {
      setProducts(state, data) {
        state.products = data;
      },
    }
    export const actions = {
      async getProducts({ commit }, search) {
        const result = await fetch()
        // 在这里执行筛选逻辑
        commit('setProducts', result);
      },
    }
    export const getters = {
      getProducts(state) {
        return state.products;
      },
    };

确保在你的组件中使用 computed 属性内的 getter。

请注意,此代码基于 Vue 3

英文:

Getters are basically computed properties for the vuex store. They take the state as their first argument and other getters as their second argument. If you want to send something as parameters to the store and then perform operations on it, you would have to define an action in the store and then call that action in your component and pass whatever data you need to, to it. Getters are meant to access your vuex state and return them to your component.

Example getter:
store.js

    //initialize the store
    state: {
      todos: []
    },
    actions: {
      getTodos(/*data from component*/) {
        //set data in todos state and call this action in your component
        state.todos = //data from component
     }
    },
    getters: {
      todos(state) {
        return state.todos;
      }
    }

Edit:

Here's how your component file would look like.

component.vue

    &lt;input
      type=&quot;text&quot;
      placeholder=&quot;Search here&quot;
      v-model=&quot;search&quot;
     /&gt;

    &lt;script&gt;
    import { ref } from &#39;vue&#39;;
    import { getProducts } from &#39;../store&#39;;

    const search = ref(&#39;&#39;);

    const products = () =&gt; {
        getProducts(search.value);
    }
    &lt;/script&gt;

store.js

    export const state = () =&gt; ({
      products: [],
    });
    
    export const mutations = {
      setProducts(state, data) {
        state.products = data;
      },
    }
    export const actions = {
      async getProducts({ commit }, search) {
        const result = await fetch()
        //perform filter logic here
        commit(&#39;setProducts&#39;, result);
      },
    }
    export const getters = {
      getProducts(state) {
        return state.products;
      },
    };

Make sure to use the getter inside a computed property in your component.

Note that this code is based on Vue 3

huangapple
  • 本文由 发表于 2023年5月29日 23:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定