高度復(fù)用的彈出框組件的寫法 vue2.0

和以前的文章一樣用的vue-cli;具體的 安裝步驟這里不做詳細(xì)的解釋了;直接開始了;一定要存在其他的組件

1.新建一個(gè)alertip.vue組件;組件代碼如下

<template>
  <div class="tip">
        <p>{{alertText}}</p>
        <p><button @click="closeTip">關(guān)閉</button></p>
  </div>
</template>
<script>
    export default {
        name: 'tip',
        components: { },
        data() {
            return { }
        },
        props: ['alertText'], //接收傳的值
        methods:{
             closeTip(){
                this.$emit('closeTip');//分發(fā)事件
            } 
        },
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!-- 此處的樣式只是添加了效果,沒(méi)有進(jìn)行準(zhǔn)確的排版,這里主要將vue的寫法!-->
<style scoped>
.tip{ width: 400px;height: 200px; overflow: hidden; position: fixed; left:38%;top:20%; background: #fff; border: 1px solid #ccc; padding:20px;} 
</style>

2.在其他組件里調(diào)用

</template>
<div>  
      <alertip v-show="alertipShow" @closeTip="alertipShow = false" :alertText="alertText"></alertip> //此處傳值,點(diǎn)擊事件
      <el-button type="primary" @click="alerts()">點(diǎn)擊此按鈕,出現(xiàn)浮動(dòng)窗口</el-button>
</div>
</template>
<script>
import alertip from './components/alertip.vue'; //此處注意組件的路徑
 export default {
        name: 'app',
        components: {
            alertip
        },
       data() {
            return {
                alertipShow:false,
                alertText:''
            }
        },
         methods: {
            alerts(){
                this.alertText="此處的值為傳過(guò)去的值,不同的頁(yè)面用的同一個(gè)組件,但是彈出的內(nèi)容是不同的!,這個(gè)信息在app組件里";
                this.alertipShow = true;
            },  
     }
}       
</script>

其他頁(yè)面同樣的方法可以調(diào)用!展示的效果是一樣的,但是不同組件可以傳入的值可以是不同的,展示的內(nèi)容也是不同的,但是效果彈窗是一樣的!

源碼見(jiàn):https://github.com/sky-xsk/vue2.0-start-

給加個(gè)“star”哦!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容