1.全局组件的目录

2.loading/index.js


import LoadingComp from './Loaiding'
const compName=LoadingComp.name //获取组件的名字 当做全局组件名字使用 const loading = {
  //Vue.use()方法就会自动执行install
 
  install(Vue) { 
    Vue.component(compName, LoadingComp)
  } }
export default loading

3.loading/Loading.vue

<template>
<div>
<h3>{{loadingMessage}}</h3>
<h3>{{aa}}</h3>
</div>
</template>
<script>
export default {
name: "Loading",
props: {
aa: {
type: String,
}
},
data() {
return {
loadingMessage: "loading..."
};
},
methods: {}, };
</script>
<style lang="scss" scoped>
</style>

4.main.js全局注册

import Loading from './components/globalComponents/loading/index'
Vue.use(Loading)

5.使用

<template>
<div>
<loading></loading> //无需注册 直接使用
</div>
</template>

批量导入多个自定义全局组件

index.js文件(上面globalComponents下面统一新建一个index.js文件)

import loading from './loading'
import mySelect from './myselect' export default {
loading,
mySelect
}
//main.js 批量导入全局自定义组件
import globalComponents from './components/globalComponents'
for (let key in globalComponents) {
Vue.use(globalComponents[key])
}

自定义全局TarBar组件

<!-- TabBar.vue-->
<template>
<div
class="tab"
v-if="defaultTarBar.textList.length!==0"
:style="{width:defaultTarBar.textList.length*45+'px'}"
>
<span
v-for="(item, index) in defaultTarBar.textList"
:key="index"
:class="[defaultTarBar.active===index?'active':'']"
@click="tabClick(index,item)"
>{{item}}</span>
</div>
</template>
<script>
export default {
name: "TabBar",
props: {
tabBar: {
type: Object,
required: true
}
},
data() {
return {
defaultTarBar: {
textList: [],
active: 0
}
};
},
methods: {
initDefaultTarBar() {
this.defaultTarBar={...this.defaultTarBar,...this.tabBar} },
tabClick(index, item) {
// console.log(index, item);
this.defaultTarBar.active = index;
this.tabBar.active = index;
}
},
created() {
this.initDefaultTarBar();
}
};
</script>
<style lang="less" scoped>
.tab {
display: flex;
flex-flow: nowrap row;
justify-content: space-between;
align-items: center;
// width: 100px;
background: #1f4b74;
overflow: hidden;
border-radius: 30px;
cursor: pointer;
text-align: center;
position: absolute;
right: 0;
top: 0; span {
flex: 1; //平分
color: #5addff;
} .active {
background: #5addff;
color: #070931;
}
}
</style>
//index.js
import TabBar from './TabBar'
const compName = TabBar.name //获取组件的名字 当做全局组件名字使用
console.log("compName", compName)
const tabBar = {
//Vue.use()方法就会自动执行install install(Vue) {
Vue.component(compName, TabBar)
} }
export default tabBar

使用tarbar.vue

自定义Swiper全局组件

<!--1Swiper使用与两组不同的数据,点击切换不同的数据源 但是两组数据的展示效果是一样的。
2.另外也是将一个很长的数据list[],切了segment段,分列一起滚动。
3. 下载安装 vue-seamless-scroll 单文件引用
-->
<template>
<div class="container" v-if="computedListData.length!==0">
<div v-for="( segmentList,index) in computedListData" :key="index">
<scroll
:data="segmentList"
:class-option="defaultSwiper.optionSetting"
class="seamless-warp"
>
<ul class="item">
<li class="li" v-for="item in segmentList" :key="item">{{item}}</li>
</ul>
</scroll>
</div>
</div>
</template>
<script>
// import vueSeamless from "vue-seamless-scroll"; import serviceCompany from "@/data/cityCell2-1/serviceCompany"; //对接情况 单位
import service from "@/data/cityCell2-1/service"; //对接情况 服务 let {
totalCount: serviceCompanyTotalCount,
list: serviceCompanyList
} = serviceCompany.data;
let { totalCount: serviceTotalCount, list: serviceList } = service.data; import scroll from "vue-seamless-scroll"; //导入组件 export default {
name: "Swiper",
props: {
swiper: {
type: Object
}
},
components: {
scroll
},
data() {
return {
serviceCompanyList: serviceCompanyList, //公司(单位)列表 15
serviceList: serviceList, //服务列表 40个
defaultSwiper: {
optionSetting: {
step: 0.4, // 数值越大速度滚动越快 limitMoveNum: 7, // 开始无缝滚动的数据量 this.dataList.length hoverStop: false, // 是否开启鼠标悬停stop direction: 1, // 0向下 1向上 2向左 3向右 openWatch: true, // 开启数据实时监控刷新dom singleHeight: 0, // 单步运动停止的高度(默认值0是无缝不停止的滚动) direction => 0/1 singleWidth: 0, // 单步运动停止的宽度(默认值0是无缝不停止的滚动) direction => 2/3 waitTime: 1000 // 单步运动停止的时间(默认值1000ms)
},
segment: 4,
active: 0 //配合tarbar切换的
}
};
},
computed: {
computedListData() {
const obj = {
0: this._segmentArr(
this.serviceCompanyList,
this.defaultSwiper.segment
),
1: this._segmentArr(this.serviceList, this.defaultSwiper.segment)
};
return obj[this.defaultSwiper.active];
}
},
methods: {
initDefaultSwiper() {
this.defaultSwiper = { ...this.defaultSwiper, ...this.swiper };
}
},
created() {
this.initDefaultSwiper();
},
watch: {
"swiper.active": {
handler(newVal) {
this.defaultSwiper.active = newVal;
}
}
}
};
</script>
<style lang="less" scoped>
.container {
display: flex;
flex-flow: nowrap row;
justify-content: space-between;
align-items: flex-start;
padding: 0.9rem 0.2rem 0.3rem 0.2rem; .seamless-warp {
height: 87%;
overflow: hidden;
.item {
text-align: left;
.li {
padding-left: 10px;
font-size: 14px;
margin-bottom: 0.2rem;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
position: relative; &:before {
content: "";
width: 4px;
height: 4px;
background: rgba(90, 221, 255, 0.3);
position: absolute;
top: 9px;
left: 0;
}
}
}
}
}
</style>
//index.js
import Swiper from './Swiper'
const compName = Swiper.name //获取组件的名字 当做全局组件名字使用
const swiper = {
//Vue.use()方法就会自动执行install install(Vue) {
Vue.component(compName, Swiper)
} }
export default swiper

use

07vue 自定义全局组件 通用流程的更多相关文章

  1. vue2 自定义全局组件(Loading加载效果)

    vue2 自定义全局组件(Loading加载效果) github地址: https://github.com/ccyinghua/custom-global-component 一.构建项目 vue ...

  2. Vuejs自定义全局组件--loading

    不管是使用框架,还是不使用任何的框架,我们都不可避免的需要与“加载中……”打交道,刚刚学习了Vuejs自定义组件的写法,就现学现卖,介绍一下吧! 先看一下目录结构,一般情况下,每一个组件都新建一个新的 ...

  3. 8svg 自定义全局组件

    0.https://www.npmjs.com/package/vue2-svg-icon 直接使用vue2-svg-icon插件 .如果不使用,就使用下面用法 注意:用阿里图标时候,最好都选择#ff ...

  4. vue自定义全局组件(自定义插件)

    有时候我们在做开发的时候,就想自己写一个插件然后就可以使用自己的插件,那种成就感很强.博主最近研究element-ui和axios的时候,发现他们是自定义组件,但是唯一有一点不同的是,在用elemen ...

  5. vue 自定义全局组件

  6. 自定义vue全局组件use使用(解释vue.use()的原理)

    我们在前面学习到是用别人的组件:Vue.use(VueRouter).Vue.use(Mint)等等.其实使用的这些都是全剧组件,这里我们就来讲解一下怎么样定义一个全局组件,并解释vue.use()的 ...

  7. 自定义vue全局组件use使用、vuex的使用

    自定义vue全局组件use使用(解释vue.use()的原理)我们在前面学习到是用别人的组件:Vue.use(VueRouter).Vue.use(Mint)等等.其实使用的这些都是全剧组件,这里我们 ...

  8. Django---FORM组件.FORM组件的字段,FORM组件校验流程,FORM组件的全局和局部钩子,FORM和Model的组合

    Django---FORM组件.FORM组件的字段,FORM组件校验流程,FORM组件的全局和局部钩子,FORM和Model的组合 一丶FORM的介绍 1.生成页面可用的HTML标签 2.对用户提交的 ...

  9. Vue 使用use、prototype自定义自己的全局组件

    使用Vue.use()写一个自己的全局组件. 目录如下: 然后在Loading.vue里面定义自己的组件模板 <template> <div v-if="loadFlag& ...

随机推荐

  1. 痞子衡嵌入式:一种i.MXRT下从App中进入ROM串行下载模式的方法

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT下在App中利用ROM API进ISP/SDP模式的方法. 我们知道i.MXRT系列分为两大阵营:CM33内核的i.MXRT ...

  2. C#,js和sql实用技巧选1

    我刚开始.net 开发的那几年,差不多每天坚持搜集实用的技巧和代码片断.几年下来也搜集了上千条.现在选出一些不太容易找或者自己有较多体会的,写在这里.内容太多,分两次发. 1.上传文件超过设置允许的最 ...

  3. MySQL是如何实现事务的ACID

    前言 最近在面试,有被问到,MySQL的InnoDB引擎是如何实现事务的,又或者说是如何实现ACID这几个特性的,当时没有答好,所以自己总结出来,记录一下. 事务的四大特性ACID 事务的四大特性AC ...

  4. 使用pip安装模块,出现Cannot unpack file xxx的问题的解决

    在windows下使用pip 豆瓣源安装gevent时出现错误 解决办法: pip install -i https://pypi.douban.com/simple/ --trusted-host ...

  5. 在vue中使用echarts报错Cannot read property getAttribute of null

    报错信息如下: 报错代码: mounted() { // ... this.drwaCharts() // drawCharts方法为自己定义的包含渲染 echarts 图表的方法 // ...} 之 ...

  6. SparkStreaming架构

    SparkStreaming是一个对实时数据流进行高通量.容错处理的流式处理系统,可以对多种数据源(如Kdfka.Flume.Twitter.Zero和TCP 套接字)进行类似Map.Reduce和J ...

  7. .Net Core中的诊断日志DiagnosticSource讲解

    前言     近期由于需要进行分布式链路跟踪系统的技术选型,所以一直在研究链路跟踪相关的框架.作为能在.Net Core中使用的APM,SkyWalking自然成为了首选.SkyAPM-dotnet是 ...

  8. IDEA_Shelve代码搁置与恢复

    日常开发中,经常会遇到在当前分支开发到一半,但是需要Checkout上个版本解决bug或调查问题的情况.这个时候,我们是将代码提到Push远程?还是直接Rollback? 最理想的做法,就是将当前的开 ...

  9. magento paypal测试配置

    1.登录https://developer.paypal.com 2.在https://developer.paypal.com/developer/accounts/中创建

  10. C++ IO的一些注意点

    读入这个坑一直以来都深受其麻烦,把遇到一些注意点记一下吧. 1.getchar读入 以前练线段树的时候做到Acwing#246 Interval GCD(原题在CodeHunter上,人懒就在Acwi ...