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. MySql实现 split

    substring_index(str,delim,count)       str:要处理的字符串       delim:分隔符       count:计数 例子:str=www.baidu.c ...

  2. Bytom Dapp 开发笔记(二):开发流程

    简介 这章的内容详细分析一下涉及智能合约Dapp的整个开发流程,注意是涉及只能合约,如果你只要一些基本转BTM功能没有太大意义,本内容补充一下官方提供的 比原链DAPP开发流程,详细实践过好踩到的一些 ...

  3. Alink漫谈(十七) :Word2Vec源码分析 之 迭代训练

    Alink漫谈(十七) :Word2Vec源码分析 之 迭代训练 目录 Alink漫谈(十七) :Word2Vec源码分析 之 迭代训练 0x00 摘要 0x01 前文回顾 1.1 上文总体流程图 1 ...

  4. 深度强化学习:Policy-Based methods、Actor-Critic以及DDPG

    Policy-Based methods 在上篇文章中介绍的Deep Q-Learning算法属于基于价值(Value-Based)的方法,即估计最优的action-value function $q ...

  5. AndroidStudio新建项目报错build failed

    AndroidStudio新建项目报错build failed 报错信息 org.gradle.initialization.ReportedException: org.gradle.interna ...

  6. jQuery的小测试

    1.在div元素中,包含了一个<span>元素,通过has选择器获取<div>元素中的<span>元素的语法是? $('div:has(span)'); 2.在&l ...

  7. vue调起微信JSDK 扫一扫,相册等需要注意的事项

    在VUE里面需要注意的第一个问题就是路由得设置成 2:第二个就是 跳转路由的时候 不要用this.$router.push 或者this.$router.replace  前者在ios 和安卓端都调不 ...

  8. FCIS:Fully Convolutional Instance-aware Semantic Segmentation

    论文:Fully Convolutional Instance-aware Semantic Segmentation   目录 0.简介 1.Position-sensitive Score Map ...

  9. 火题大战Vol.1 A.

    火题大战Vol.1 A. 题目描述 给定两个数\(x\),\(y\),比较\(x^y\) 与\(y!\)的大小. 输入格式 第一行一个整数\(T\)表示数据组数. 接下来\(T\)行,每行两个整数\( ...

  10. RFC2474 - Definition of the Differentiated Services Field (DS Field) in the IPv4 and IPv6 Headers的双语版

    RFC2474 - Definition of the Differentiated Services Field (DS Field) in the IPv4 and IPv6 Headers英文版 ...