Use vue to realize the implementation code of clicking the button to slide out of the panel

  • 2021-07-10 18:26:16
  • OfStack

It is easy to make mistakes when communicating, or the information can't be transmitted at all. Then here is Example 1, how to complete the click event through the communication between components.

In the index. vue file:


<div>
   <el-button type="primary" @click="onShow"> Order me </el-button>
</div>

Delivery mediation


<addForm :show="formShow" @onHide="formShow = false"></addForm>

Imported component, that is, the component to be popped up


import addForm from './docsForm'
export default {
components: {
addForm
},
data() {
return {
show: false,
formShow: false
}
},
watch: {
 show: {
      handler: function(newVal, oldVal) {
        this.show = newVal
      },
      deep: true
    }
},
methods: {
onShow() {
      this.formShow = true
    }
}
}

This is the method in this file.

Then there is the pop-up component docsForm. How does vue upload data


<slidePanel :width="550" :show="show" title=" Add knowledge " @changeShow="hide">
<div class="docs-body">
</div>
</slidePanel>
export default {
props: {
show: false
},
methods: {
hide() {
      this.$emit('onHide')
    },
}

In the component slidePanel. vue


<template>
  <transition name="slide-panel-fade">
    <div v-if="show" class="slide-panel" :style="{ width: width + 'px'}">
      <div class="slide-panel-layout">
        <div class="slide-panel-header">
          <h3 class="slide-panel-header-title">{{title}}</h3>
          <button class="slide-panel-header-close" @click="onShow"><i class="el-icon-close"></i></button>
        </div>
        <div class="slide-panel-body">
          <slot></slot>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  props: {
    title: String,
    show: Boolean,
    width: {
      type: Number,
      default: 500
    }
  },
  methods: {
    onShow() {
      this.$emit('changeShow', false)
    }
  },
  watch: {
    show: {
      handler: function(newVal, oldVal) {
        this.show = newVal
      },
      deep: true
    }
  },
  mounted() {
    document.body.appendChild(this.$el)
  },
  destroyed() {
    this.$el.remove()
  }
}
</script>

In this way, the book pull pop-up between different components can be realized.


Related articles: