需要注意的是,如果改為動態(tài)綁定圖片,請參考:vue-cil和webpack中本地靜態(tài)圖片的路徑問題解決方案
我這里將靜態(tài)資源文件轉(zhuǎn)移到了static目錄下面。
重構(gòu)后的代碼如下:
<template> <div> <div class="swiper-container" ref="slider"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="slide in slides"> <router-link :to="{name:'BookDetail',params:{id:slide.id}}"> <img :src="slide.img_url"/> </router-link> </div> </div> </div> </div> </template> <script> import 'swiper/dist/css/swiper.css' import Swiper from 'swiper' export default { name: "Slider", data(){ return{ slides:[{id:1,img_url:'./static/sliders/t1.svg'},{id:2,img_url:'./static/sliders/t2.svg'}] } }, mounted(){ new Swiper (this.$refs.slider, { loop: true, // 如果需要分頁器 pagination: '.swiper-pagination', // 如果需要前進(jìn)后退按鈕 nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', // 如果需要滾動條 scrollbar: '.swiper-scrollbar', }) } } </script>
這里還沒有把組件完全獨立,里面有數(shù)據(jù)定義,其實可以把這個數(shù)據(jù)作為一個參數(shù)傳遞進(jìn)來,也就是組件之間數(shù)據(jù)傳遞。
通過路由傳參,在router/index.js中定義路由
export default new Router({ routes: [ { name:'BookDetail', path:'/books/:id', component: BookDetail } ] })
前面的輪播組件中已經(jīng)定義了需要傳遞的路由參數(shù)
<router-link :to="{name:'BookDetail',params:{id:slide.id}}"> <img :src="slide.img_url"/> </router-link>
參數(shù)接收界面BookDetail.vue
<template><p> 點擊的是:<span v-text="id"></span></p></template><script> export default { name: "BookDetail", data(){ return{ id:this.$route.params.id } }, props:[] }</script><style scoped></style>
如果傳遞參數(shù)太多,這樣的方式肯定不方便,那么可以采用vuex,或者組件數(shù)據(jù)傳遞。
關(guān)于組件傳值可以參考:Vue 組件之間傳值
關(guān)于Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開問題
之后每次運行:hs即可。
相關(guān)推薦:
jQuery自適應(yīng)輪播圖插件Swiper用法示例
使用swiper組件實現(xiàn)輪播廣告效果
Vue封裝Swiper實現(xiàn)圖片輪播效果的代碼分享
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com