使用 nuxt 开发网站 之 常用插件 swiper、toast、axios
- axios 插件 是最常用的啦,这里用的是nuxt 给的列子,没啥特别的,够用,
1 export default function ({ $axios}) { 2 $axios.onRequest((config) => { 3 return config 4 }) 5 $axios.onResponse((response) => { 6 if (response.status == 200) { 7 return response.data 8 } 9 }) 10 $axios.onError(err => { 11 return Promise.reject(err) 12 }) 13 }
当然如果有需要一些特别的处理,比如header 上需要带参数,你也可以自己create,官方文档里说的很清楚了,这里不再重复。
- toast 插件 也是需要的呢,我这里用的是ui 组件使用的是 bootstrap-vue :
1 import Vue from 'vue' 2 import { ToastPlugin } from 'bootstrap-vue' 3 4 Vue.use(ToastPlugin)
- swiper 插件 说起这个插件,开始使用时因为安装时,依赖版本不匹配,还有写小波折。当然这里已经把坑填好了:安装依赖 swiper@5.2.0 vue-awesome-swiper@^4.1.1 这里版本你好已经标出来了哦,直接安装后使用时会有问题,一定得注意。
1 import Vue from 'vue' 2 import VueAwesomeSwiper from 'vue-awesome-swiper' 3 import 'swiper/css/swiper.min.css' 4 5 6 Vue.use(VueAwesomeSwiper)
- 这里我封装了一个 fullscreenBanner 组件也可以分享给大家,全屏轮播组件:
1 <template> 2 <swiper :options="options" style="width:100%"> 3 <swiper-slide v-for="(item,index) in bannerList" :key="index"> 4 <b-link href="#" class="nav-link p-0"> 5 <!-- 图片 --> 6 <b-img v-if="item.type == 'image'" :src="item.image" class="banner-media"></b-img> 7 <video v-else :src="item.image" class="banner-media" loop muted autoplay ></video> 8 <!-- 文字内容 --> 9 <div v-if="item.title || item.sub_text" class="carousel-caption"></div> 10 <div class="banner-content" v-if="item.title || item.sub_text"> 11 <p class="title" v-html="item.title"></p> 12 <p class="subtitle" v-html="item.sub_text"></p> 13 </div> 14 </b-link> 15 </swiper-slide> 16 <div class="swiper-pagination banner" slot="pagination"></div> 17 </swiper> 18 </template> 19 20 <script> 21 export default { 22 name:"fullscreenBanner", 23 props:{ 24 options:{ 25 type:Object, 26 default:()=>{ 27 return{ 28 slidesPerView: 1, 29 watchSlidesVisibility:true, 30 observer: true,//修改swiper自己或子元素时,自动初始化swiper 31 observeParents: true,//修改swiper的父元素时,自动初始化swiper 32 // loop:true, 33 // loopAdditionalSlides:0,//loop模式下会在slides前后复制若干个slide,,前后复制的个数不会大于原总个数。 34 on: { 35 slideChangeTransitionStart: function() { 36 let cur = this.slides[this.activeIndex].children[0].children[0] // 当前 37 let pre = this.slides[this.previousIndex].children[0].children[0] // 前一个 38 if(pre.tagName == 'VIDEO'){ 39 pre.pause() // 暂停之前的 40 } 41 if(cur.tagName == 'VIDEO'){ 42 cur.play() // 当前的自动播放 43 } 44 } 45 }, 46 pagination: { 47 el: '.swiper-pagination', 48 clickable: true //点击分页器的指示点分页器控制Swiper切换 49 }, 50 } 51 } 52 }, 53 bannerList:{ 54 type:Array, 55 default:()=>{ 56 return [] 57 } 58 } 59 }, 60 data(){ 61 return{ 62 63 } 64 } 65 } 66 </script> 67 68 <style lang="scss" scoped> 69 .banner{ 70 bottom: 3.75rem; 71 } 72 .banner /deep/ .swiper-pagination-bullet{ 73 box-sizing: border-box; 74 border: none; 75 width: 0.75rem; 76 height: 0.75rem; 77 background: #FFFFFF; 78 opacity: 0.6; 79 border-radius: 50%; 80 margin-right: 0.94rem !important; 81 margin-left: 0.94rem !important; 82 transition: 0.3s; 83 } 84 .banner /deep/ .swiper-pagination-bullet-active{ 85 width: 3.38rem; 86 height: 0.63rem; 87 background: #FE5800; 88 border-radius: 1rem; 89 opacity: 1; 90 } 91 .banner-media{ 92 width:100%; 93 height: 100vh; 94 object-fit: cover; 95 display: block; 96 } 97 .banner-content{ 98 position: absolute; 99 right: 16%; 100 bottom: 20px; 101 left: 16%; 102 z-index: 10; 103 padding-top: 20px; 104 padding-bottom: 20px; 105 color: #fff; 106 text-align: center; 107 transform: translateY(-50%); 108 top: calc(50% + 6rem); 109 110 .title{ 111 font-size: 4.5rem; 112 font-family: AvantGarde Bk BT; 113 font-weight: bold; 114 color: #FFFFFF; 115 line-height: 5rem; 116 white-space: pre-wrap; 117 } 118 .subtitle{ 119 font-size: 1.5rem; 120 font-family: AvantGarde Bk BT; 121 font-weight: normal; 122 color: #EEEEEE; 123 line-height: 2.25rem; 124 white-space: pre-wrap; 125 } 126 } 127 .carousel-caption{ 128 background: #070A11; 129 opacity: 0.4; 130 left: 0; 131 top: 0; 132 height: 100%; 133 bottom: 0; 134 right: 0; 135 z-index: 2; 136 } 137 </style>
就到这里,大家可以安心使用。
作者:胡倩倩0903
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
posted on 2021-02-19 10:10 kitty20180903suzhou 阅读(586) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY