首页 > 解决方案 > 如何从另一个组件打开对话框?

问题描述

我有一个对话框组件,如下所示:

<template>
  <q-dialog v-model="confirm" persistent>
    <q-card>
      <q-card-section class="row items-center">
        <span class="q-ml-sm">You must register</span>
      </q-card-section>
      <q-card-actions align="right">
        <q-btn flat label="Save" color="primary" v-close-popup/>
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  export default {
    name: 'UserInfo',
    data() {
      return {
        confirm: false,
      }
    },

    created: function () {

    },
    methods: {
      show_dialog() {
        this.confirm = true;
      }

    }
  }
</script>

另一个导入上述组件的组件:

<template>
  <div class='q-pa-md' style='max-width: 300px'>
    <UserInfo></UserInfo>
  </div>
</template>


<script>
  import UserInfo from "pages/UserInfo";

  export default {
    name: 'PageIndex',
    components: {UserInfo},
    data() {
      return {

      }
    },
    mounted() {

    },
    created: function () {
      const config = {
        headers: {
          'Authorization': `Bearer ${this.$kc.token}`,
          'Content-Type': 'application/json',
        }
      };

    console.log(this.$kc);


      this.$axios.get(`http://localhost:9090/user/${this.$kc.subject}`)
        .then((res) => {
          console.log(res)
        })
        .catch(_ => {
          //Here the dialog should open
        })

    },
    methods: {

    }
  }
</script>

Dialog应该在catch块中调用。

如何触发catch块中的打开事件?

标签: javascriptvue.jsquasar-frameworkquasar

解决方案


Change the confirm property to props instead of data, and manage the toggle from the father component.

father component:

<template>
  <div class='q-pa-md' style='max-width: 300px'>
    <UserInfo :confirm="isConfirm" @changeConfirm="changeConfirm"></UserInfo>
  </div>
</template>


export default {
   data() {
       return {
          isConfirm: ''
       }
   },
   methods: {
       changeConfirm(status) {
           this.isConfirm= status
       }
   },
   created: function () {
      //...
      this.$axios.get(`http://localhost:9090/user/${this.$kc.subject}`)
        .then((res) => {
           console.log(res)
         })
        .catch(_ => {
            this.isConfirm= false
     })
   }
}

child component:

export default {
    props: {
       confirm: ''
    },
    methods: {
       show_dialog() {
         this.$emit('changeConfirm', true);
       }
    }
}

推荐阅读