使用 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="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      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      3.38rem;
     86     height: 0.63rem;
     87     background: #FE5800;
     88     border-radius: 1rem;
     89     opacity: 1;
     90 }
     91 .banner-media{
     92     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>
    View Code

    就到这里,大家可以安心使用。

作者:胡倩倩0903
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/kitty-blog/p/14411893.html