Vue在提交表单时不呈现UI更改

2sbarzqh  于 2023-06-24  发布在  Vue.js
关注(0)|答案(1)|浏览(103)

这可能是一个愚蠢的问题,但是每当我向productsList提交一个表单以发布新产品时,UI并不会立即呈现它,只有在我重新加载页面之后才会呈现。我正在发布数据并从firebase获取
ProductList.vue

<template>
  <div class="product-list">
    <NewItemForm></NewItemForm>
    <ul>
      <ProductListItem
        v-for="product in productsList"
        v-bind:key="product.product.id"
        :title="product.product.title"
        :price="+product.product.price"
        :id="product.product.id"
      />
    </ul>
  </div>
</template>

<script>
import ProductListItem from "./ProductListItem.vue";
import NewItemForm from "./NewItemForm.vue";
import { mapActions, mapGetters } from "vuex";

export default {
  name: "ProductList",
  components: {
    ProductListItem,
    NewItemForm,
  },

  methods: {
    ...mapActions(["getProducts"]),
  },

  computed: { ...mapGetters(["productsList"]) },

  created() {
    this.getProducts();
  },
};
</script>

store.js

import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

export const store = new Vuex.Store({
  state: {
    products: [],
  },

  mutations: {
    addNewProduct: (state, product) => {
      product.id = product.name;
      state.products.unshift(product);
    },
    setProducts: (state, products) => {
      for (const name in products) {
        products[name].product.id = name;
        state.products = [...state.products, products[name]];
      }
    },
  },

  getters: {
    productsList: (state) => state.products,
  },

  actions: {
    async addNewProduct({ commit }, product) {
      const response = await axios.post(
        "https://vue-s-261e8-default-rtdb.firebaseio.com/products.json",
        product
      );
      commit("addNewProduct", response.data);
    },

    async getProducts({ commit }) {
      try {
        const response = await axios.get(
          "https://vue-s-261e8-default-rtdb.firebaseio.com/products.json"
        );
        commit("setProducts", response.data);
      } catch (err) {
        console.log(err);
      }
    },
  },
});

NewItemForm.vue

<template>
  <div>
    <form class="form">
      <label for="name">Product name</label>
      <input type="text" name="name" v-model="product.title" />
      <label for="description">Product description</label>
      <input type="text" name="description" v-model="product.description" />
      <label for="price">Product price</label>
      <input type="text" name="price" v-model="product.price" />
    </form>
    <button class="button" type="submit" v-on:click.prevent="onSubmit">
      Add new item
    </button>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  name: "NewItemForm",

  data() {
    return {
      product: {
        title: "",
        description: "",
        price: "",
      },
    };
  },

  methods: {
    ...mapActions(["addNewProduct"]),
    onSubmit() {
      this.addNewProduct({
        product: this.product,
      });
    },
  },
};
</script>

我试着设置一个观察者,但这只会引入一个无限循环。

rjee0c15

rjee0c151#

Vue 2在状态方面有一些奇怪的行为,特别是在处理数组时。我相信在这种情况下,你需要使用Vue.set,你可以从下面的链接中找到更多细节,在那里你还可以找到更多关于Vue 2中React性的文档。
https://v2.vuejs.org/v2/guide/reactivity.html
您的示例(ProductListItem.vue)中似乎还缺少一个组件
我建议升级到Vue 3并利用Pinia,因为它现在是VueX的替代品。
如果你能上传一个完整的问题库,我很乐意看一看。
编辑:
查看存储库后,这看起来像修复:
addNewProduct突变改为:

addNewProduct: (state, product) => {
    product.id = product.name;
    // this particular change might not be required
    // feel free to try without
    state.products = [...state.products, product]
},

最后,将addNewProduct操作更改为:

async addNewProduct({ commit }, product) {
    await axios.post(
      "https://vue-s-261e8-default-rtdb.firebaseio.com/products.json",
       product
    );
    commit("addNewProduct", product);
}

这将修复您的React性问题并正确更新您的阵列。希望这有帮助:)

相关问题