vue-cli5遇到的一丢丢webpack的打包配置(修改视频的目录)&轮播插件swiper在vue中的使用

一丢丢webpack的打包配置

 

首先在根目录中找到vue.config.js,这就是本项目的配置文件,可以看到初始是:

1 const { defineConfig } = require('@vue/cli-service')
2 module.exports = defineConfig({
3   transpileDependencies: true
4 })

我遇到的问题:

1.需求要求把mp4视频文件打包到img文件目录下

2.打包后index.html页面报错文件路径找不到

 

第一个问题的参考文档:

1.vue-cli中webpack相关:https://cli.vuejs.org/zh/guide/webpack.html#%E9%93%BE%E5%BC%8F%E6%93%8D%E4%BD%9C-%E9%AB%98%E7%BA%A7

2.webpack-chain(webpack链式配置文档):https://github.com/neutrinojs/webpack-chain

但是此时此刻我哪知道webpack的rule里mp4是哪个?于是请教了师傅,得到了一个小tips:

3.执行 output.js这个命令,会发现根目录生成了output.js文件,打开就能看到具体的配置,可以按需复制到配置文件中进行修改

比如我搜mp4,就搜到了它的校验是在media文件夹下的,怪不得打包以后视频单独搞了个文件夹,问题解决!

 

但是其实有一个更加规范的方法,那就是用url-loader进行配置:

参考文档:https://blog.csdn.net/sinat_41212418/article/details/121914931

贴一下示例代码:

 

 

 

第二个问题:

直接百度到了,配置publicPath为"./",直接修改资源引入的路径。

 

于是代码被改为:

const {
  defineConfig
} = require('@vue/cli-service')
module.exports = defineConfig({
  publicPath: "./",
  transpileDependencies: true,
  chainWebpack: config => {
    config.module.rule('images').test(/\.(mp4|png|jpe?g|gif|webp|avif)(\?.*)?$/)
    config.module.rule('media').test(/\.(webm|ogg|mp3|wav|flac|aac)(\?.*)?$/)
  },
})

 

其实就是把image文件的校验多加了个mp4,把原本media的校验去掉了mp4,大功告成!

 

swiper插件在vue中的使用

 

因为swiper高版本不太稳定,所以这里用了^4.5.1版本的。一开始不知道,直接下了最新版,没想到都到8.0了,干脆写了装4.0的,附上卸载和安装指定版本的代码:

npm uninstall swiper
npm uninstall vue-awesome-swiper
npm install swiper@4 --save-dev
npm install vue-awesome-swiper@3 --save-dev

 

 

再附上简单使用的代码:

  1 <template>
  2   <div class="PictureBanner">
  3     <div class="PictureBanner-bg">
  5       <div class="swiper-container">
  6         <div class="swiper-wrapper">
  7           <div v-for="item in imgList" class="swiper-slide" v-bind:key="item">
  8             <img :alt="item" :src="item" />
  9           </div>
 10         </div>
 11         <div class="swiper-pagination" id="pagination">
 12           <ul class="pagination"></ul>
 13         </div>
 14         <div class="swiper-button-next"></div>
 15         <div class="swiper-button-prev"></div>
 16       </div>
 17     </div>
 18   </div>
 19 </template>
 20 
 21 <script>
 22 import Swiper from 'swiper';
 23 import 'swiper/dist/js/swiper'
 24 import 'swiper/dist/css/swiper.min.css';
 25 
 26 export default {
 27   name: 'PictureBanner',
 28   data: function () {
 29     return {
 30       imgList: [
 31         require("./img/module6_banner_1.png"),
 32         require("./img/module6_banner_2.png"),
 33         require("./img/module6_banner_3.png"),
 34         require("./img/module6_banner_4.png"),
 35         require("./img/module6_banner_5.png"),
 36       ]
 37     }
 38   },
 39   mounted() {
 40     setTimeout(() => {
 41       const swiper = new Swiper('.swiper-container', { // eslint-disable-line no-unused-vars
 42         autoplay: 3000,
 43         loop: true,
 44         pagination: {
 45           el: '.swiper-pagination',
 46           bulletClass: 'my-swiper-pagination-bullet',
 47           bulletActiveClass: 'my-swiper-pagination-bullet-active',
 48           clickable: true,
 49           renderBullet: function (index, className) {
 50             return (
 51               '<span class="' + className + '" ></span>'
 52             );
 53           },
 54         },
 55         navigation: {
 56           nextEl: '.swiper-button-next',
 57           prevEl: '.swiper-button-prev',
 58         }
 59       })
 60     }, 1000);
 61   },
 62   methods: {
 63     // 自定义单页方法
 64     slideTo(type) {
 65       if (type === 'prev') {
 66         this.swiper.slidePrev()
 67       } else {
 68         this.swiper.slideNext()
 69       }
 70 
 71       setTimeout(() => {
 72         this.swiper.autoplay.start()
 73       }, 2000)
 74     },
 75   }
 76 }
 77 </script>
 78 
 79 <style scoped>
 80 .PictureBanner {
 81   position: relative;
 82   width: 100%;
 83   text-align: center;
 84   margin-bottom: 8.8rem;
 85 }
 86 
 87 .PictureBanner-bg {
 88   width: 21rem;
 89   background: url(@/assets/descreiption_bg_6.png) no-repeat;
 90   background-size: contain;
 91   padding-left: 1.46rem;
 92   box-sizing: border-box;
 93   padding-top: 0.6rem;
 94   padding-right: 1rem;
 95   margin: 0 auto;
 96 }
 97 
 98 .swiper-container {
 99   position: initial;
100 }
101 
102 .swiper-button-next,
103 .swiper-button-prev {
104   top: 47%;
105   width: 2.4rem;
106   height: 3.5rem;
107 }
108 
109 .swiper-button-next {
110   right: 0;
111 }
112 
113 .swiper-button-prev {
114   left: 0;
115 }
116 
117 .swiper-button-next:after,
118 .swiper-button-prev:after {
119   content: '';
120   position: absolute;
121   top: 0;
122   right: 0;
123   bottom: 0;
124   left: 0;
125 }
126 
127 .swiper-button-next:after {
128   background: transparent url(./img/module6_button_r.png) no-repeat;
129   background-size: 100%;
130 }
131 
132 .swiper-button-prev,
133 .swiper-container-rtl .swiper-button-next {
134   background: transparent url(./img/module6_button_l.png) no-repeat;
135   background-size: 100%;
136 }
137 
138 .swiper-button-next,
139 .swiper-container-rtl .swiper-button-prev {
140   background: transparent url(./img/module6_button_r.png) no-repeat;
141   background-size: 100%;
142 }
143 
144 .swiper-button-prev:after {
145   background: transparent url(./img/module6_button_l.png) no-repeat;
146   background-size: 100%;
147 }
148 
149 .swiper-slide img {
150   width: 100%;
151   height: 100%;
152 }
153 
154 .swiper-pagination {
155   display: flex;
156   justify-content: center;
157   align-items: center;
158   vertical-align: middle;
159   bottom: -2rem;
160 }
161 </style>
162 
163 <style>
164 #pagination .my-swiper-pagination-bullet {
165   display: inline-block;
166   width: 0.6rem;
167   height: 0.6rem;
168   background-color: #999;
169   background: url(./img/module6_dot_unchecked.png) no-repeat;
170   background-size: 100%;
171   margin: 0 0.4rem;
172   background-position: center;
173 }
174 
175 #pagination .my-swiper-pagination-bullet-active {
176   display: inline-block;
177   width: 1.2rem;
178   height: 1.2rem;
179   background-color: #fff;
180   background: url(./img/module6_dot_checked.png) no-repeat;
181   background-size: 100%;
182   background-position: center;
183 }
184 </style>

 

相关部分的页面效果:

 

 

个人的配置是替换了左右切换箭头和分页的图标,一开始遇到pagination样式不生效的问题,后来发现是因为style标签里加了scoped(局部作用域,样式只针对单独的文件),去掉后即可生效~

posted @ 2022-06-14 17:22  芝麻小仙女  阅读(1621)  评论(0编辑  收藏  举报