当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue 模板封装 传递数据

vue 模板封装 传递数据

2020年07月08日  | 移动技术网IT编程  | 我要评论

vue项目长用到的模板封装(使用移动端ui插件Vant)

目录中1处的轮播图会在项目中频繁使用 特将次封装以备候用

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网