【问题标题】:Close Vuetify dialog with Vuex使用 Vuex 关闭 Vuetify 对话框
【发布时间】:2020-09-10 14:20:18
【问题描述】:

经过数小时的搜索并试图找到正确的方法,我的 n00b 大脑爆炸了。 我已经尝试了很多事情,以至于我完全迷失了。一切都如我所愿,可以删除我想要的客户,前端刷新等。除了对话框。

你能解释一下如何关闭这个对话框吗?

这是我的对话。

<template>
  <div>
    <template>
      <tbody>
        <tr v-for="customer in AllCustomers" :key="customer.id" class="todo">
          <td>{{customer.ID}}</td>
          <td>{{ customer.name }}</td>
          <td>{{customer.telephone}}</td>
          <td>{{customer.email}}</td>
          <v-btn color="success" @click="showDeleteDialog(customer)">DELETE</v-btn>
        </tr>
      </tbody>
    </template>
    <v-dialog v-model="dialogDelete" persistent max-width="500px">
      <v-card>
        <v-card-title>Delete</v-card-title>
        <v-card-text>Weet je zeker dat je {{customerToDelete}} wenst te verwijderen?</v-card-text>
        <v-card-actions>
          <v-btn color="primary" text @click="close">Annuleer</v-btn>
          <v-btn color="primary" text @click="deleteCustomer(customer.ID)">Verwijderen</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </div>
</template>



<script>
import { mapGetters, mapActions, mapState } from "vuex";

export default {
  name: "AllCustomers",

  data() {
    return {
      customerToDelete: "",
      dialogDelete: false
    };
  },

  methods: {
    ...mapActions(["fetchAllCustomers", "deleteCustomer"]),

    async close() {
      this.dialogDelete = false;
    },

    async showDeleteDialog(customer) {
      this.customer = Object.assign({}, customer);
      this.customerToDelete = this.customer.name;
      this.dialogDelete = !this.dialogDelete;
      this.$store.commit("toggleDialog");
    }
  },

  computed: mapGetters(["AllCustomers"]),
  created() {
    this.fetchAllCustomers();
  },
  ...mapState(["dialogDelete"])
};
</script>

这里是我的模块 js。

import axios from 'axios';

const state = {
    customers: [],
    dialogDelete: false
};

const getters = {
    AllCustomers: state => state.customers
};

const actions = {
    async fetchAllCustomers({ commit }) {
        const response = await axios.get(
            'http://localhost:8888'
        );
        console.log(response.data.data);
        commit('setAllCustomers', response.data.data);
    },

    async deleteCustomer({ commit }, id) {
        await axios.delete(`http://localhost:8888/delete`, {
            data: {
                id: id
            }
        })
        console.log(id)
        commit('removeCustomer', id, this.dialogDelete = false);
    },

}

const mutations = {
    setAllCustomers: (state, customers) => (state.customers = customers),
    removeCustomer: (state, id) =>
        (state.customers = state.customers.filter(customer => customer.ID !== id)),
}

export default {
    state,
    getters,
    actions,
    mutations
};

【问题讨论】:

  • 看起来 close() 没有定义。

标签: vue.js vuex vuetify.js vuex-modules


【解决方案1】:

由于您没有在代码中包含 &lt;script&gt; 标记,我假设您正试图通过来自 vue 组件的关闭事件直接切换 vuex 状态,这不会以这种方式工作。 相反,您可能希望调度一个提交更改 vuex 状态的更改的操作。

不过,更好的办法是将对话框组件封装在一个单独的 vue SFC(单文件组件)中,该组件具有本地状态isActive,您可以通过本地方法 this.isActive = false 将其打开或关闭,一次你导入那个组件然后给它一个引用ref="deleteDialog",你就可以像这样访问组件的内部方法:this.$refs.deleteDialog.close()

有关参考的更多信息,请参阅docs

编辑

例如:

DialogDelete.vue

<template>
    <v-dialog v-model="isActive" persistent max-width="500px">
      <v-card>
        <v-card-title>Delete</v-card-title>
        <v-card-text>Weet je zeker dat je {{customerToDelete}} wenst te verwijderen?</v-card-text>
        <v-card-actions>
          <v-btn color="primary" text @click="close">Annuleer</v-btn>
          <v-btn color="primary" text @click="deleteCustomer(customer.ID)">Verwijderen</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
</template>

<script>
  export default {
    data() {
      return {
        isActive: false
      }
    },
    methods: {
      close() {
        this.isActive = false
      },
      open() {
        this.isActive = true
      }
    },
  }
</script>

Parent.vue

<template>
  <div>
    <v-btn @click="openDialog">Open Dialog</v-btn>
    <dialog-delete ref="deleteDialog" />
  </div>
</template>

<script>
  export default {
    components: {
      dialogDelete: () => import("./DialogDelete.vue"),
    },

    methods: {
      openDialog() {
        this.$refs.deleteDialog.open()
      }
    },
  }
</script>

我也很清楚,您没有将 vuex 辅助方法(如 mapGettersmapState)放在它们应该在的位置,例如 mapStatemapGetters 都应该在 @987654337 内@:

computed: {
  ...mapGetters(["getterName"]),
  ...mapState(["stateName"])
}

查看vuex docs

【讨论】:

  • 我现在使用脚本进行了更新。你介意告诉我你的解决方案吗?
  • 我已经按照你说的做了,但是当我按下“打开对话框”时,我得到了 this.$refs.deleteDialog.open() 不是函数的错误....
  • 谢谢你,但它不起作用。我的一切都一样,但仍然出现同样的错误
【解决方案2】:

您应该使用 mapState 从存储中获取您的 dialogDelete 变量:

 // in your dialog
 import { mapState } from "vuex"

 computed: {
 ...mapState(["dialogDelete"])
 }

您应该通过提交更改其突变状态:

// in vuex store
const mutations = {
setAllCustomers: (state, customers) => (state.customers = customers),
removeCustomer: (state, id) =>
    (state.customers = state.customers.filter(customer => customer.ID !== 
id)),
toggleDialog: (state) => (state.dialogDelete = !state.dialogDelete)    
}

// in your dialog
this.$store.commit("toggleDialog")

【讨论】:

  • 谢谢,但是 this.$store.commit("toggleDialog") 去哪儿了?
  • 这是在 vuex 突变中触发方法的方式。 $store 指的是 Vuex.Store,它控制状态、动作、getter 和突变。
  • 是的,我知道。但我的意思是我是否将这部分放在代码中?你在对话框中说但是在哪里
  • 如果您只想通过单击关闭来关闭对话框窗口,只需设置 @click="dialogDelete = false"> 或者您可以从以下方法调用 this.$store.commit("toggleDialog")你想打开或关闭它。
  • 我不希望只是关闭对话框,它还需要实际需要从数据库中删除该项目。这有效,除了对话框没有关闭是问题。所以我正在努力将这两个动作结合起来。
猜你喜欢
  • 2018-09-12
  • 2019-08-05
  • 2021-02-25
  • 2021-03-28
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多