从VueJS中的组件模板打开Vuetify对话框

时间:2017-12-30 15:33:16

标签: javascript vuejs2 vue-component vuetify.js

我正在使用VueJS {{3}},我需要从另一个模板打开一个对话框 - 它作为组件模板导入。单击 App.vue 中的 菜单按钮后,应打开模态。 这是我的设置:

  • App.vue =带有菜单按钮的导航模板
  • Modal.vue =模态模板,在main.js中导入为全局

main.js

import Modal from './components/Modal.vue'
Vue.component('modal', Modal)

Modal.vue模板:

<template>
  <v-layout row justify-center>
    <v-btn color="primary" dark @click.native.stop="dialog = true">Open Dialog</v-btn>
    <v-dialog v-model="dialog" max-width="290">
      <v-card>
        <v-card-title class="headline">Use Google's location service?</v-card-title>
        <v-card-text>Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Disagree</v-btn>
          <v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Agree</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </v-layout>
</template>
<script>
  export default {
    data () {
      return {
        dialog: false
      }
    }
  }
</script>

如何打开对话框?

8 个答案:

答案 0 :(得分:38)

不需要事件总线和v-model

更新

当我第一次回答这个问题时,我把答案称为“解决方法”,因为当时感觉并不完全“正确”,而且我是Vue.js的新手。我想使用 v-model 指令打开或关闭对话框,但我无法到达那里。一段时间后,我发现how to do this in the docs,使用输入事件值属性,这就是我认为应该在没有事件总线的情况下完成的。

父组件:

<template>
<v-dialog v-model="show" max-width="500px">
  <v-card>
    <v-card-actions>
      <v-btn color="primary" flat @click.stop="show=false">Close</v-btn>
    </v-card-actions>
  </v-card>
</v-dialog>
</template>

<script>
export default {
  props: {
     value: Boolean
  },
  computed: {
    show: {
      get () {
        return this.value
      },
      set (value) {
         this.$emit('input', value)
      }
    }
  }
}
</script>

子组件(ScheduleForm):

<template>
   <v-btn color="accent" large @click.stop="showScheduleForm=true">    
   <ScheduleForm :visible="showScheduleForm" @close="showScheduleForm=false" />
</template>

<script>
import ScheduleForm from '~/components/ScheduleForm'

export default {
  data () {
    return {
      showScheduleForm: false
    }
  },
  components: {
    ScheduleForm
  }
}
</script>

原始答案:

我能够在不需要全局事件总线的情况下解决这个问题。

我使用了带有getter和setter的计算属性。由于Vue警告你直接改变父属性,因此在setter中我只是向父节点发出了一个事件。

以下是代码:

父组件:

<template>
<v-dialog v-model="show" max-width="500px">
  <v-card>
    <v-card-actions>
      <v-btn color="primary" flat @click.stop="show=false">Close</v-btn>
    </v-card-actions>
  </v-card>
</v-dialog>
</template>

<script>
export default {
  props: ['visible'],
  computed: {
    show: {
      get () {
        return this.visible
      },
      set (value) {
        if (!value) {
          this.$emit('close')
        }
      }
    }
  }
}
</script>

子组件(ScheduleForm):

{{1}}

答案 1 :(得分:9)

您可以使用自定义事件并使用event bus for non parent-child communication打开对话框。

如果您的应用程序变得更复杂,我建议您使用Vuex for state management

活动总线解决方案:

main.js 或新文件中创建并导出新的Vue实例:

export const bus = new Vue()

app.vue 中导入bus并发出事件:

<template>
  <div>
    <button @click.prevent="openMyDialog()">my button</button>
  </div>
</template>

<script>
  import {bus} from '../main' // import the bus from main.js or new file
  export default {
    methods: {
      openMyDialog () {
        bus.$emit('dialog', true) // emit the event to the bus
      }
    }
  }
</script>

modal.vue 中也导入总线并在创建的钩子中侦听事件:

<script>
  import {bus} from '../main'    
  export default {
    created () {
      var vm = this
      bus.$on('dialog', function (value) {
        vm.dialog = value
      })
    }
  }
</script>

答案 2 :(得分:7)

  

有很多方法可以做到这一点,比如Vuex,Event Bus,Props,你可以用它来管理模态是否必须打开或关闭。我将使用.sync修饰符向你展示我最喜欢的方式:< / p>

首先,我将简化您的问题(代码部分)

父组件

<template>
   <div>
     <button @click="dialog=true">Open Dialog</button>
     <Child :dialog.sync="dialog" />
   </div>
</template>

<script>
import Child from './Child.vue'
export default {
    components: {
      Child
    },
    data: {
      return {
        dialog: false
      }
   }
}
</script>

子(对话)组件

<template>
  <v-layout row justify-center>
    <v-dialog v-model="dialog" persistent max-width="290">
      <v-card>
        <v-card-title class="headline">Use Google's location service?</v-card-title>
        <v-card-text>Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.</v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="green darken-1" flat @click.native="close">Close</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </v-layout>
</template>

<script>

  export default {
    props: {
        dialog: {
        default: false
      }
    },
    methods: {
        close() {
        this.$emit('update:dialog', false)
      }
    }
  }

</script>

答案 3 :(得分:2)

The most simpler way I found to do it is:

in data() of component, return a attribute, let's say, dialog.

When you include a component, you can set a reference to your component tag. E.g.:

import Edit from '../payment/edit.vue';

<edit ref="edit_reference"></edit>

Then, inside my component, I have set a method:

        open: function () {
            var vm = this;

            vm.dialog = true;
        }

Finally, I can call it from parent, using:

  editar(item)
  {
      var vm = this;

      vm.$refs.edit_reference.open();
  }

答案 4 :(得分:2)

简单的最小工作示例

codepen

value道具作为value传递到v-dialog组件,并在子对话框中随时关闭input事件:

//CustomDialog.vue
<v-dialog :value="value" @input="$emit('input')">
  <v-btn color="red" @click.native="$emit('input')">Close</v-btn>
</v-dialog>
...
props:['value']

并将v模型添加到您的父母

//Parent.vue
<custom-dialog v-model="dialog">

因此,没有自定义事件总线,没有data,没有watch,没有computed

答案 5 :(得分:1)

App.vue template添加此内容

<modal></model>

它会使用Modal.vuev-btn

呈现您当前的v-dialog模板

现在,当你点击该模式时,将会有一个button - Open Dialog

答案 6 :(得分:0)

我更喜欢使用这个:

DialogConfirm.vue

<template>
  <v-dialog :value="visible" max-width="450">
    <v-card>
      <v-card-title v-text="title" />
      <v-card-text v-text="message" />
      <v-card-actions v-if="visible">
        <template v-for="action in value">
          <v-spacer :key="action.label" v-if="typeof action == 'string'" />
          <v-btn
            v-else
            text
            :key="action.label"
            v-text="action.label"
            @click="doAction(action.action)"
            :color="action.color"
          />
        </template>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>
<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component';
import { Prop, Watch } from 'vue-property-decorator';

@Component
export default class DialogConfirm extends Vue {

  @Prop({ type: String, default: "Confirm" })
  title: string

  @Prop({ type: String, default: "Are you sure?" })
  message: string

  @Prop({ type: Array, default: undefined })
  value: { label: string, action: () => boolean, color: string }[]

  get visible() {
    return Array.isArray(this.value) && this.value.length > 0
  }

  doAction(action: () => boolean) {
    if ('undefined' == typeof action || action() !== false) {
      this.$emit('input', null)
    }
  }
}
</script>

用法示例

/** Disable AP Mode */
  setApMode(enable: boolean) {
    const action = () => {
      Api.get('wifi', {
        params: {
          ap: enable
        }
      }).then(response => this.$store.dispatch('status'))
    }
    if (enable == true) {
      // No confirmation
      return action();
    }
    this.dialogTitle = 'Are you sure?'
    this.dialogMessage = "you may lost connection to this device.";
    this.dialogActions = [
      {
        label: 'Cancel',
        color: 'success'
      },
      'spacer',
      {
        label: "OK, Disable it",
        color: "error",
        action
      }
    ]
  }

答案 7 :(得分:0)

methods: {
  openDialog(e) {
    this.dialog = true;
  }
},

这个对我有用