另一个组件的Vue调用方法

Jon*_* xx 6 javascript vue.js vuejs2

我有两个组件,一个卡片组件和一个模态组件,模态组件包含模态元素,在我的卡片组件中,我想要一个按钮来打开我的模态组件的模态窗口。我该如何管理,到目前为止我已经做到了:

我的卡片组件:

<template>
    <v-layout v-if="visible">
        <v-flex xs12 sm6 offset-sm3>
            <v-card>
                <v-card-title primary-title>
                    <div>
                        <h3 class="headline mb-0">test</h3>
                        <div>test</div>
                    </div>
                </v-card-title>

                <v-divider light></v-divider>

                <v-card-actions>
                <v-btn
                    color="primary"
                    dark
                    @click="dialog = true"
                >
                Open Dialog
                </v-btn> <!-- open modal dialog of modal component? -->

                    <tenant-number-modal></tenant-number-modal>
                </v-card-actions>

                <input type="hidden" id="tenant-id" :value=tenantId>
            </v-card>
        </v-flex>
    </v-layout>
</template>

<script>
    export default {
        data () {
            return {
                visible: true,
                dialog: false,
                uniqueTenantNumber: ''
            }
        },
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我的模态组件:

<template>
    <v-layout row justify-center>
        <v-btn
            color="primary"
            dark
            @click="dialog = true"
        > <!-- this calls the modal but only in this component -->
        Open Dialog
        </v-btn>

        <v-dialog
            v-model="dialog"
            max-width="290"
        >
            <v-card>
                <v-card-title class="headline">test</v-card-title>
            </v-card>
        </v-dialog>
    </v-layout>
</template>

<script>
  export default {
    data () {
      return {
        dialog: false
      }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

Jns*_*Jns 8

您可以使用ref.

    <v-card-actions>
      <v-btn
       color="primary"
       dark
       @click="openModal">
      Open Dialog
      </v-btn> <!-- open modal dialog of modal component? -->
      <tenant-number-modal ref="modal"></tenant-number-modal>
    </v-card-actions>
...
<script>
   export default {
      data () {
         return {
            //visible: true,
            //dialog: false,
            //uniqueTenantNumber: ''
         }
      },
      methods: {
         openModal() {
            this.$refs.modal.showModal();
         }
      }
   }
</script>
Run Code Online (Sandbox Code Playgroud)

您的模态组件:

<template>
    <v-layout row justify-center>
    ...
        <v-dialog
            v-model="dialog"
            max-width="290">
            <v-card>
                <v-card-title class="headline">test</v-card-title>
            </v-card>
        </v-dialog>
    </v-layout>
</template>

<script>
  export default {
    data () {
      return {
        dialog: false
      }
    },
    methods: {
       showModal() {
       this.dialog = true;
       }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)


Get*_*awn 6

您可以做的是创建一个事件总线。这将允许您一次向 1+ 个组件发送消息。发出消息后,所有正在侦听的组件都将执行。

首先你需要创建总线:

总线.js

import Vue from 'vue';
export const EventBus = new Vue();
Run Code Online (Sandbox Code Playgroud)

Next 在将发出您调用的消息的组件中 EventBus.$emit(name, data)

组件A.js

import { EventBus } from './bus.js';

export default {
  methods: {
    emitGlobalClickEvent() {
      EventBus.$emit('i-got-clicked', 'extra data');
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的其他组件中,你只需要监听事件。以下可以添加到组件中,你只需要使用EventBus.$on(name, handle)或者如果你只想听一次使用EventBus.$once(name, handle)。

组件B.js

import { EventBus } from './bus.js';

export default {
  created() {
    EventBus.$on('i-got-clicked', data => {
      console.log(data)
      // You can then call your method attached to this component here
      this.openModal()
    });
  },
  methods: {
    openModal() {
      console.log('I am opening')
    }
  }
}
Run Code Online (Sandbox Code Playgroud)