注意:安装时建议使用手机热点,wifi不稳定,会出现执行不成功的现象。
安装
使用包管理器
# 选择一个你喜欢的包管理器
# NPM
npm install element-plus --save
# Yarn
yarn add element-plus
# pnpm
pnpm install element-plus
引入配置
完整引入
如果你对打包后的文件大小不是很在乎,那么使用完整导入会更方便。
// main.ts
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
全局配置
在引入 ElementPlus 时,可以传入一个包含 size
和 zIndex
属性的全局配置对象。 size
用于设置表单组件的默认尺寸,zIndex
用于设置弹出组件的层级,zIndex
的默认值为 2000
。
完整引入:
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus, { size: 'small', zIndex: 3000 })
使用
https://element-plus.org/zh-CN/component/overview.html
组件:Carousel走马灯——复制代码。
官网代码
<template>
<div class="block text-center">
<span class="demonstration">
Switch when indicator is hovered (default)
</span>
<el-carousel height="150px">
<el-carousel-item v-for="item in 4" :key="item">
<h3 class="small justify-center" text="2xl">{{ item }}</h3>
</el-carousel-item>
</el-carousel>
</div>
<div class="block text-center" m="t-4">
<span class="demonstration">Switch when indicator is clicked</span>
<el-carousel trigger="click" height="150px">
<el-carousel-item v-for="item in 4" :key="item">
<h3 class="small justify-center" text="2xl">{{ item }}</h3>
</el-carousel-item>
</el-carousel>
</div>
</template>
<style scoped>
.demonstration {
color: var(--el-text-color-secondary);
}
.el-carousel__item h3 {
color: #475669;
opacity: 0.75;
line-height: 150px;
margin: 0;
text-align: center;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
background-color: #d3dce6;
}
</style>
自己修改后
<template>
<div class="swiper">
<div class="block text-center">
<span class="demonstration">
Switch when indicator is hovered (default)
</span>
<el-carousel height="470px">
<el-carousel-item v-for="(item, index) in SwiperData" :key="index">
<!-- 使用 img 标签来显示图片 -->
<img :src="item.img" alt="slide" />
</el-carousel-item>
</el-carousel>
</div>
<!-- <div class="block text-center" m="t-4">
<span class="demonstration">Switch when indicator is clicked</span>
<el-carousel trigger="click" height="150px">
<el-carousel-item v-for="item in 4" :key="item">
<h3 class="small justify-center" text="2xl">{{ item }}</h3>
</el-carousel-item>
</el-carousel>
</div> -->
</div>
</template>
<script>
import { ref } from "vue";
export default {
name: "LayOut",
// 组件逻辑
setup() {
const SwiperData = ref([
{ img: require("@/assets/images/slide1.jpg") },
{ img: require("@/assets/images/slide2.jpg") },
{ img: require("@/assets/images/slide3.jpg") },
]);
return {
SwiperData,
};
},
};
</script>
<style>
.swiper {
width: 590px;
height: 470px;
margin: 0 auto;
}
/* 组件样式 */
.demonstration {
display: none;
color: var(--el-text-color-secondary);
}
.el-carousel__item h3 {
color: #475669;
opacity: 0.75;
line-height: 150px;
margin: 0;
text-align: center;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
background-color: #d3dce6;
}
</style>
自定义配置
Carousel API
Carousel Attributes
属性名 说明 类型 Default height carousel 的高度 string
‘’ initial-index 初始状态激活的幻灯片的索引,从 0 开始 number
0 trigger 指示器的触发方式 enum
hover autoplay 是否自动切换 boolean
true interval 自动切换的时间间隔,单位为毫秒 number
3000 indicator-position 指示器的位置 enum
‘’ arrow 切换箭头的显示时机 enum
hover type carousel 的类型 enum
‘’ cardScale 2.7.8 当 type 为 card时,二级卡的缩放大小 number
0.83 loop 是否循环显示 boolean
true direction 展示的方向 enum
horizontal pause-on-hover 鼠标悬浮时暂停自动切换 boolean
true motion-blur 2.6.0 添加动态模糊以给走马灯注入活力和流畅性。 boolean
false