简体   繁体   English

Vue JS:方法样式 Getter(过滤)和对 state 变化的反应

[英]Vue JS : Method Style Getter (to filtering) AND reactivity to state change

I worked on a simple schedule application and i use Vuex as a state manager.我开发了一个简单的日程安排应用程序,我使用 Vuex 作为 state 经理。

I want a way to filter the task state, to only return specific date and category tasks (Method Style Getter ig?) but i also want the component which get these tasks (via Method Style Getter) to react (so rerender) every time the task state change.我想要一种方法来过滤任务 state,只返回特定的日期和类别任务(Method Style Getter ig?)但我也希望获取这些任务的组件(通过 Method Style Getter)每次都做出反应(因此重新渲染)任务 state 更改。

What is the most effective way to get there?到达那里最有效的方法是什么?


Access the tasks访问任务

export default {
  name: "TasksContainer",
  methods: {
    logSomething() {
      console.log("new task (good)");
    },
  },
  components: {
    Task,
  },
  computed: {
    ...mapGetters(["weekCourseTasks", "courseTasks", "nowMoment"]),
  },
};

Update tasks更新任务

export default {
  name: "AddTask",
  data() {
    return {
      taskName: "",
      taskDetail: "",
      taskCourse: "",
      taskDate: "",
    };
  },
  methods: {
    ...mapActions(["addTask"]),
    postForm() {
      this.addTask({
        taskName: this.taskName,
        taskDetail: this.taskDetail,
        taskCourse: this.taskCourse,
        taskDate: this.taskDate,
      });

      this.$emit("new-task");
    },
  },
};

If I understand you correctly, you would like to have tasks, that you can filter based on the date and other categories.如果我理解正确的话,你会希望有任务,你可以根据日期和其他类别进行过滤。 Vuex allows for such getter methods. Vuex 允许这样的 getter 方法。

The following code block allows you to write getters based on parameters.以下代码块允许您根据参数编写 getter。

getters: {
// ...
  getTaskByDate: (state) => (date) => {
    return state.tasks.find(task => task.date === date)
  }
}

In order to see them:为了看到他们:

store.getters.getTaskByDate(xyz);

This should allow you to filter your state and make the component responsive.这应该允许您过滤 state 并使组件响应。

A full documentation can be found here under the part "Method-Style Access"可以在此处的“方法样式访问”部分下找到完整的文档

Additionally, be sure to perform your updates according to the guidelines to preserve reactivity.此外,请务必根据指南执行更新以保持反应性。

Since a Vuex store's state is made reactive by Vue, when we mutate the state, Vue components observing the state will update automatically.由于 Vuex 商店的 state 被 Vue 响应,当我们改变 state 时,观察 state 的 Vue 组件将自动更新。 This also means Vuex mutations are subject to the same reactivity caveats when working with plain Vue:这也意味着 Vuex 突变在使用普通 Vue 时受到相同的反应性警告:

Prefer initializing your store's initial state with all desired fields upfront.最好预先使用所有需要的字段初始化商店的初始 state。

When adding new properties to an Object, you should either:向 Object 添加新属性时,您应该:

Use Vue.set(obj, 'newProp', 123), or使用 Vue.set(obj, 'newProp', 123),或者

Replace that Object with a fresh one.将 Object 换成新的。 For example, using the object spread syntax we can write it like this:例如,使用 object 传播语法,我们可以这样写:

state.obj = { ...state.obj, newProp: 123 }

(From the Vuex mutations documentation ) (来自 Vuex突变文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM