vue 全局组件封装

vue中写好一个组件功能

<template>
  <div id="app">
        <div class="popwin">
            <p class="info">{{info}}</p>
            <button class="close_popwin" @click="close_popwin">关闭</button>
        </div>
  </div>
</template>

<script>
export default {
  name: 'popwin',
//外部使用传来的数据
    props:{
        info: String,
    },
    methods:{
//所有的事件逻辑在使用方那里处理,组件内部只告知发生了事件
        close_popwin(){
            this.$emit("close_popwin",'')
        }
    }
}
</script>

<style scoped>
.popwin{
   width: 300px;
   height: 70px;
   background: #000;
   opacity: 0.7; 
   position:relative;
   display: flex;
   justify-content: space-between;
   align-items: center;
   position: fixed;
   left:200px;
   top:50%;
}
.close_popwin{
    color: red;
    border: none;
    background-color: transparent;
    outline: none;
    cursor: pointer;  
}
.info{
    color: #fff;
    line-height: 70px;
    font-size: 14px;
}
</style>

新建index.js文件,文件名自己取

//全局组件导出

import popwin from './popwin.vue'

const Popwin = {
    install:function(Vue){
//第一个参数是name 第二个是引入的组件
        Vue.component('popwin',popwin)
    }
}
export default Popwin

main.js全局注册:

//引入js
import Popwin from './components/view/index'
Vue.use(Popwin)

组件中使用:

<template>
  <div id="app">
    <!-- 全局组件 -->
      <popwin info='局部地区多云转晴' @close_popwin='popwin_close' v-show="shows"></popwin>
  </div>
</template>

<script>

export default {
  name: 'App',
  data(){
    return{
      shows:true,
    }
  },
  methods:{
     popwin_close(){
        this.shows = false
      }
  }
}
</script>

<style scoped>
    
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值