使用Vue 3、Pinia和TypeScript创建一个轮播图组件的实际操作步骤
来源:亿速云
时间:2024-03-27 15:27:16 325浏览 收藏
文章小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《使用Vue 3、Pinia和TypeScript创建一个轮播图组件的实际操作步骤》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
为什么封装?
迎合es6模块化开发思想
注册为全局组件,可以更好地复用,需要用到的地方,直接使用标签即可
静态结构 后面再进行更改
<script lang="ts" setup name="XtxCarousel">
defineProps()
</script>
<template>
<div class="xtx-carousel">
<ul class="carousel-body">
<li class="carousel-item fade">
<RouterLink to="/">
<img
src="https://cache.yisu.com/upload/information/20220725/112/220.jpg"
alt=""
/>
</RouterLink>
</li>
<li class="carousel-item">
<RouterLink to="/">
<img
src="https://cache.yisu.com/upload/information/20220725/112/220.jpg"
alt=""
/>
</RouterLink>
</li>
<li class="carousel-item">
<RouterLink to="/">
<img
src="https://cache.yisu.com/upload/information/20220725/112/220.jpg"
alt=""
/>
</RouterLink>
</li>
</ul>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn prev"
><i class="iconfont icon-angle-left"></i
></a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn next"
><i class="iconfont icon-angle-right"></i
></a>
<div class="carousel-indicator">
<span class="active"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
</template>
<style scoped lang="less">
.xtx-carousel {
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
.carousel {
&-body {
width: 100%;
height: 100%;
}
&-item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
transition: opacity 0.5s linear;
&.fade {
opacity: 1;
z-index: 1;
}
img {
width: 100%;
height: 100%;
}
}
&-indicator {
position: absolute;
left: 0;
bottom: 20px;
z-index: 2;
width: 100%;
text-align: center;
span {
display: inline-block;
width: 12px;
height: 12px;
background: rgba(0, 0, 0, 0.2);
border-radius: 50%;
cursor: pointer;
~ span {
margin-left: 12px;
}
&.active {
background: #fff;
}
}
}
&-btn {
width: 44px;
height: 44px;
background: rgba(0, 0, 0, 0.2);
color: #fff;
border-radius: 50%;
position: absolute;
top: 228px;
z-index: 2;
text-align: center;
line-height: 44px;
opacity: 0;
transition: all 0.5s;
&.prev {
left: 20px;
}
&.next {
right: 20px;
}
}
}
&:hover {
.carousel-btn {
opacity: 1;
}
}
}
</style>请求数据都存放在pinia里面
import { defineStore } from 'pinia'
import request from '@/utils/request'
import { BannerItem, IApiRes } from '@/types/data'
export default defineStore('home', {
persist: {
enabled: true
},
state() {
return {
bannerList: [] as BannerItem[]
}
},
actions: {
// 拿轮播图数据
async getBannerList() {
const res = await request.get<IApiRes<BannerItem[]>>('/home/banner')
console.log('拿轮播图数据', res)
this.bannerList = res.data.result
}
}
})类型检测
// 所有的接口的通用类型
export interface IApiRes<T> {
msg: string,
result: T
}
// 轮播图数据中的项
export type BannerItem = {
id: string;
imgUrl: string;
hrefUrl: string;
type: string;
}页面级组件
<script lang="ts" setup>
import useStore from '@/store';
const { home } = useStore()
// 发请求
home.getBannerList()
</script>
<template>
<div class="home-banner">
<!-- 轮播图子组件 -->
<XtxCarousel :slides="home.bannerList" :autoPlay="true" :duration="1000"/>
</div>
</template>
<style scoped lang="less">
.home-banner {
width: 1240px;
height: 450px;
position: absolute;
left: 0;
top: 0;
z-index: 98;
background-color: #ccc;
}
/deep/ .xtx-carousel .carousel-btn.prev {
left: 270px !important;
}
/deep/ .xtx-carousel .carousel-indicator {
padding-left: 250px;
}
</style>全局组件
<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data'
import { onBeforeUnmount, onMounted, ref } from 'vue';
// 定义props
const { slides, autoPlay, duration } = defineProps<{
slides: BannerItem[],
autoPlay?: boolean, // 是否开启自动播放
duration?: number // 自动播放的间隔时间
}>()
// 当前哪个图片选中 高亮
const active = ref(1)
// 点击右侧箭头++
const hNext = () => {
active.value++
if(active.value === slides.length){
active.value = 0
}
}
// 点击左侧箭头--
const hPrev = () => {
active.value--
if(active.value < 0){
active.value = slides.length - 1
}
}
// 如果开启了自动播放,则每隔duration去播放下一张: hNext()
onMounted(() => {
start()
})
onBeforeUnmount(() => {
stop()
})
let timer = -1
// 鼠标离开要继续播放
const start = () => {
if(autoPlay) {
timer = window.setInterval(() => {
hNext()
}, duration)
}
}
// 鼠标hover要暂停播放
const stop = () => {
clearInterval(timer)
}
</script>
<template>
<div class="xtx-carousel" @mouseleave="start()" @mouseenter="stop()">
<ul class="carousel-body">
<li class="carousel-item"
:class="{ fade: idx === active}"
v-for="(it, idx) in slides" :key="it.id">
<RouterLink to="/">
<img :src="it.imgUrl" alt="" />
</RouterLink>
</li>
</ul>
<a @click="hPrev" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
<a @click="hNext" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
<div class="carousel-indicator">
<span
v-for="(it, idx) in slides"
:class="{ active: active===idx}"
@mouseenter="active = idx"
></span>
</div>
</div>
</template>
<style scoped lang="less">
.xtx-carousel {
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
.carousel {
&-body {
width: 100%;
height: 100%;
}
&-item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0; // 不可见
transition: opacity 0.5s linear;
&.fade {
opacity: 1; // 可见
z-index: 1;
}
img {
width: 100%;
height: 100%;
}
}
&-indicator {
position: absolute;
left: 0;
bottom: 20px;
z-index: 2;
width: 100%;
text-align: center;
span {
display: inline-block;
width: 12px;
height: 12px;
background: rgba(0, 0, 0, 0.2);
border-radius: 50%;
cursor: pointer;
~ span {
margin-left: 12px;
}
&.active {
background: #fff;
}
}
}
&-btn {
width: 44px;
height: 44px;
background: rgba(0, 0, 0, 0.2);
color: #fff;
border-radius: 50%;
position: absolute;
top: 228px;
z-index: 2;
text-align: center;
line-height: 44px;
opacity: 0;
transition: all 0.5s;
&.prev {
left: 20px;
}
&.next {
right: 20px;
}
}
}
&:hover {
.carousel-btn {
opacity: 1;
}
}
}
</style>以上就是《使用Vue 3、Pinia和TypeScript创建一个轮播图组件的实际操作步骤》的详细内容,更多关于TypeScript,Vue3,pinia的资料请关注golang学习网公众号!
声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
299 收藏
-
349 收藏
-
398 收藏
-
410 收藏
-
280 收藏
-
297 收藏
-
476 收藏
-
142 收藏
-
179 收藏
-
122 收藏
-
404 收藏
-
201 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习