vue 模板封装 传递数据
程序员文章站
2022-06-15 12:46:19
vue项目长用到的模板封装(使用移动端ui插件Vant)lubotu.vue代码
vue项目长用到的模板封装(使用移动端ui插件Vant)
lubotu.vue代码
<template>
<div class="lunboimage">
<van-swipe :autoplay="3000">
<van-swipe-item v-for="image in images" :key="image.id">
<img v-lazy="image.img" />
</van-swipe-item>
</van-swipe>
</div>
</template>
<script>
export default {
name: "Lunboimage",
data() {
return {
};
},
props:['images']
};
axio简易二次封装
//response.js
import axios from 'axios'
axios.defaults.baseURL = '####.com/'
function myFetch(data={}){
if(!Object.prototype.toString.call(data)==='[object Object]'){return alert('data is wrong')}
if(!data.method){ data.method='get'}
if(data.method=='get'){
return axios.get(data.url,{params:data.params})
}else{ return axios.post(data.url,data.params)}
}
export default myFetch
//api.js
import myFetch from '../utils/response.js'
export const getthumimages = (imgid) => {
return myFetch({
url: '/api/getthumimages/' + imgid,
method: 'get'
})
}
页面中需要的使用lubotu.vue 例
<template>
<div class="goodsDetail">
<div style="padding:10px">
<Lunboimage :images="picture"></Lunboimage>
</div>
</div>
</template>
<script>
import Lunboimage from "@/components/lubotu.vue";
//axios封装
import { getthumimages } from "@/api/api.js";
export default {
data() {
return {
// 轮播图片
picture: []
};
},
components: {
Lunboimage
},
created() {
this.initDetail();
this.$store.state.value = 1;
},
methods: {
// 初始化数据
async initDetail() {
//数据赋值
const res = await getthumimages(id);
for (let i = 0; i < res.data.message.length; i++) {
//lubotu的接口在每一个页面的不一定相同 可以自行转换
this.picture.push({ img: "" });
this.picture[i].img = res2.data.message[i].src;
}
},
本文地址:https://blog.csdn.net/x131633/article/details/107138247
上一篇: div中图片和文字同一行实现垂直居中
下一篇: 感冒发热,想流鼻涕