在Vue上显示模态

时间:2018-10-16 02:51:53

标签: vue.js vuejs2

尝试使用以下代码显示模式:

import VModal from 'vue-js-modal';

Vue.use(VModal);

在模板上:

<modal name="hello-world">
  hello, world!
</modal>

Vue部分:

myMethod() {
    this.$modal.show('hello-world');
}

当尝试调用myMethod()时,出现错误“无法读取未定义的属性'show'”。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这在vue-cli简单环境中对我有用 及其工作正常,经过测试

main.js

import App from "./App.vue";
import Vue from "vue";
import VModal from "vue-js-modal";

Vue.use(VModal);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div id="app">
    <h3>Hello world test</h3>
    <modal name="hello-world">
      hello, world!
    </modal>
    <button @click="showModal">show modal</button>
  </div>
</template>
<script>
export default {
  methods: {
    showModal() {
      this.$modal.show("hello-world");
    }
  }
};
</script>

<style lang="scss">
</style>