简体   繁体   English

如何从另一个组件打开对话框?

[英]How to open the dialog from another component?

I have a dialog component that looks as follow:我有一个对话框组件,如下所示:

<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>

And another component that import the component above:另一个导入上述组件的组件:

<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>

The Dialog should get called in the catch block. Dialog应该在catch块中被调用。

How to trigger the open event in the catch block?如何在catch块中触发 open 事件?

Change the confirm property to props instead of data , and manage the toggle from the father component.confirm属性更改为props而不是data ,并从父组件管理切换。

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);
       }
    }
}

Just add ref=“userInfo” to the <UserInfo> tag in the HTML template of the PageIndex component.只需将ref=“userInfo”添加到 PageIndex 组件的 HTML 模板中的<UserInfo>标记。 Then you can call this.$refs.userInfo.show_dialog() in the catch block然后你可以在 catch 块中调用this.$refs.userInfo.show_dialog()

Proper way to call dialogs in quasar在类星体中调用对话框的正确方法

<template>
  <q-dialog v-model="confirm" ref="dialog" 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" @click="onOKClick"/>
          <q-btn flat label="Cancel" color="primary" @click="onCancelClick"/>
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

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

    created: function () {

    },
    methods: {
      show() {
            this.$refs.dialog.show();
        },
        hide() {
            this.$refs.dialog.hide();
        },
        onDialogHide() {
            this.$emit("hide");
        },
        onOKClick() {
            //code that you want to emit or functionality you want 
        },
        onCancelClick() {
            this.hide();
        }
    }
  }
</script>

From component you want to call从您要调用的组件

<template>
  <div class='q-pa-md' style='max-width: 300px'>
    
  </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
                this.$q
                .dialog({
                    component: UserInfo,
                    parent: this,
                })
                .onOk(value => {
                    //Functionality you want on OK click of dialog
                })
                .onCancel(() => {})
                .onDismiss(() => {});
        })

    },
    methods: {

    }
  }
</script>

After that maybe you will get error of $q.dialog not known so add Dialog in your Plugins in quasar.js like this在那之后,也许你会得到 $q.dialog not known 的错误,所以在你的插件中添加对话框 quasar.js 像这样

import { Quasar, Dialog } from 'quasar'
plugins: [
        Dialog
    ],

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

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