首页 > 其他分享 >vue 全局组件封装

vue 全局组件封装

时间:2023-01-03 15:37:08浏览次数:39  
标签:popwin 封装 name Popwin vue export 组件 close


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>

 

标签:popwin,封装,name,Popwin,vue,export,组件,close
From: https://blog.51cto.com/u_12422954/5986016

相关文章

  • react高阶组件
    1.首先介绍高阶函数基本概念:函数可以作为参数被传递:函数可以作为返回值输出: 2.高阶组件组件作为参数被传递,返回值是一个组件高阶组件是一个函数案例:将A组件作为公共组件,BC......
  • React 小案例 无线首页底部导航组件
    效果:底部页面导航点击当前图标图标高亮页面结构1.将所用到的小图标文件放置在static文件夹下2.在src下新建tabbar文件夹,用于存放组件资源3.页面代码注意:将本页面的css引入......
  • react 基础 脚手架组件挂载 / 生命周期
    脚手架里的index.js配置组件的挂载importReactfrom'react'importReactDOMfrom'react-dom'//importLifefrom'./Life'importXiaojiejiefrom'./react/Xiaojiejie'......
  • vue 基础学习笔记【一】
    vue.js简介概念:构建用户界面的渐进式框架。(渐进式:不必一开始就用Vue所有的全家桶,根据场景,官方提供了方便的框架供你使用。)Vue的核心库只关注视图层。【引入vue】可以​​创......
  • vue 高德地图 即时搜索 模糊搜索 下拉搜索
    index.html里面引入amap:首先你要去  ​​https://lbs.amap.com/​​ 注册一个属于自己的key注册好账号后--点击右上角‘控制台’--左侧边栏‘应用管理--我的应用’---点......
  • react 做一个点赞按钮组件
      创建组件Like.js一开始设置为黑色false,isLiked如果为true渲染红心,false渲染黑心setState时用了两种方法importReact,{Component}from'react'exportdefaultclass......
  • vue vuex 学习小demo
    创建store.js 引入并使用vueximport Vuex from 'vuex'Vue.use(Vuex)1.使用new Vuex.Store创建({})创建store2.创建state:{}存放数据3.mutations:{}存放方法4.actions:{}......
  • vue3.0的全局api变化
    1.全局api使用的变化:vue3已经去除Vue语法,取代的是用createApp创建的app  2.其他改变2.1data函数的变化,在vue3data必须是一个函数,否则报错     2.2过......
  • vue3.0新组件
    1.fragment1.1解释和意义   1.2使用:没有特定的标签,直接不使用根标签即可2.teleport(传送)1.1解释和意义:不管嵌套多少层都可以直接进行组件传送1.2使用:t......
  • python + appium 常用公共方法封装
    appium程序下载安装见之前的帖子:https://www.cnblogs.com/gancuimian/p/16536322.htmlappium环境搭建见之前的帖子:https://www.cnblogs.com/gancuimian/p/16557576.html......