【问题标题】:Vue.js Modal component shows up in component list, but the modal doesn't actually showVue.js 模态组件显示在组件列表中,但模态实际上并未显示
【发布时间】:2021-07-17 01:54:17
【问题描述】:

我试图在单击按钮时显示项目详细信息,我已将数据正确转到模态,但我似乎无法让模态实际显示。我在模板中尝试了不同的模态实现,我得到的关闭是在按钮按下时出现灰色屏幕。我现在不知道该怎么做,因为我在模态上尝试的每个教程似乎都不适合我。我正在使用 VUE CLI 版本 2

主页.vue

<template>
  <div class="home">
    <Navbar />
    <Ordertiles @childToParent="onClickChild"/>
    <div v-if="showModal">
    <Modal @close="toggleModal" :header="item.header" :text="item.text" :image="item.img"/>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import Navbar from '@/components/Navbar.vue'
import Ordertiles from '@/components/Ordertiles.vue'
import Modal from '@/components/Modal.vue'

export default {
  name: 'Home',
  components: {
    Navbar,
    Ordertiles,
    Modal
  },
  data() {
    return {
      item: '',
      showModal: false
    }
  },

  methods: {
    toggleModal() {
      this.showModal = !this.showModal
    },
    onClickChild(item) {
      this.item = item
      console.log(this.item)
      this.toggleModal()
    }
  }
}
</script>

Modal.vue

<template>
  <div class="modal">
    <div class="container">
      <div class="modal__title">Direction</div>
      <p>{{header}}</p>
      <img v-bind:src="image"> 
      <p> {{text}} </p>
      <button class="mt-3 border-b border-teal font-semibold">Close</button>
    </div>
  </div>
</template>

<script>

export default {
  props: ['header', 'text', "image"],
  methods: {
    close () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="css" scoped>
  .modal {
    background-color: rgba(0, 0, 0, 0.7);
  }
</style>

Before Clicking the button After button click

【问题讨论】:

    标签: javascript html vue.js


    【解决方案1】:

    为什么不使用其他 npm modal 包,如 vue-modality 。我强烈保证这个包。它易于使用和理解。这将为您节省大量时间,因为您无需重新发明轮子。查看此链接https://www.npmjs.com/package/vue-modality

    【讨论】:

    • 我刚刚尝试使用 Vue-modality,但我仍然遇到同样的问题。模态组件出现了,但它不显示任何内容。当我将鼠标悬停在开发工具中的模态组件上时,它也不会显示模态的位置。
    • 使用vue-modality,你使用的是调用modal的show方法的函数吗?
    • 我并不完全希望将显示模式的方法放在哪里,目前,我想要的每个按钮/项目都在 orderTiles 组件中。我将数据发送回 Home.vue,然后让 div 带有 if 切换模式。我真的找不到放置 this.$refs.myRef.open() 的好地方。问题很可能是我很笨。
    • 我最终找到了解决我的模态问题的方法,不过感谢您的帮助!
    【解决方案2】:

    我找到了一个真正适合我的模态示例

    Modal.vue

    <template>
      <transition name="modal">
            <div class="modal-mask">
              <div class="modal-wrapper">
                <div class="modal-container">
    
                  <div class="modal-header">
                    <slot name="header">
                      default header
                    </slot>
                  </div>
    
                  <div class="modal-body">
                    <slot name="body">
                      default body
                    </slot>
                  </div>
    
                  <div class="modal-footer">
                    <slot name="footer">
                      default footer
                      <button class="modal-default-button" @click="$emit('close')">
                        OK
                      </button>
                    </slot>
                  </div>
                </div>
              </div>
            </div>
          </transition>
    </template>
    
    <script>
    
    export default {
      props: ['header', 'text', "image"],
      methods: {
        close () {
          this.$emit('close')
        }
      },
      components: {
      }
    }
    </script>
    
    <style lang="css" scoped>
      .modal-mask {
      position: fixed;
      z-index: 9998;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      display: table;
      transition: opacity 0.3s ease;
    }
    
    .modal-wrapper {
      display: table-cell;
      vertical-align: middle;
    }
    
    .modal-container {
      width: 300px;
      margin: 0px auto;
      padding: 20px 30px;
      background-color: #fff;
      border-radius: 2px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
      transition: all 0.3s ease;
      font-family: Helvetica, Arial, sans-serif;
    }
    
    .modal-header h3 {
      margin-top: 0;
      color: #42b983;
    }
    
    .modal-body {
      margin: 20px 0;
    }
    
    .modal-default-button {
      float: right;
    }
    
    .modal-enter {
      opacity: 0;
    }
    
    .modal-leave-active {
      opacity: 0;
    }
    
    .modal-enter .modal-container,
    .modal-leave-active .modal-container {
      -webkit-transform: scale(1.1);
      transform: scale(1.1);
    }
    
    </style>
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 2018-01-09
      • 2021-01-24
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      相关资源
      最近更新 更多