当前位置: 移动技术网 > IT编程>开发语言>JavaScript > vue-其他

vue-其他

2019年12月15日  | 移动技术网IT编程  | 我要评论

vue-resource

// 2.1 导入 vue-resource
import vueresource from 'vue-resource'
// 2.2 安装 vue-resource
vue.use(vueresource)
// 设置请求的根路径
vue.http.options.root = 'http://vue.studyit.io';
// 全局设置 post 时候表单数据格式组织形式   application/x-www-form-urlencoded
vue.http.options.emulatejson = true;

moment

// 导入格式化时间的插件
import moment from 'moment'
// 定义全局的过滤器
vue.filter('dateformat', function (datastr, pattern = "yyyy-mm-dd hh:mm:ss") {
  return moment(datastr).format(pattern)
})

preview

// 安装 图片预览插件
import vuepreview from 'vue-preview'
vue.use(vuepreview)

缩略图使用实例

<template>
  <div class="photoinfo-container">
    <h3>{{ photoinfo.title }}</h3>
    <p class="subtitle">
      <span>发表时间:{{ photoinfo.add_time | dateformat }}</span>
      <span>点击:{{ photoinfo.click }}次</span>
    </p>

    <hr>

    <!-- 缩略图区域 -->
    <div class="thumbs">
      <img class="preview-img" v-for="(item, index) in list" :src="item.src" height="100" @click="$preview.open(index, list)" :key="item.src">
    </div>

    <!-- 图片内容区域 -->
    <div class="content" v-html="photoinfo.content"></div>

    <!-- 放置一个现成的 评论子组件 -->
    <cmt-box :id="id"></cmt-box>
  </div>
</template>

<script>
// 1. 导入评论子组件
import comment from "../subcomponents/comment.vue";

export default {
  data() {
    return {
      id: this.$route.params.id, // 从路由中获取到的 图片id
      photoinfo: {}, // 图片详情
      list: [] // 缩略图的数组
    };
  },
  created() {
    this.getphotoinfo();
    this.getthumbs();
  },
  methods: {
    getphotoinfo() {
      // 获取图片的详情
      this.$http.get("api/getimageinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.photoinfo = result.body.message[0];
        }
      });
    },
    getthumbs() {
      // 获取缩略图
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          // 循环每个图片数据,补全图片的宽和高
          result.body.message.foreach(item => {
            item.w = 600;
            item.h = 400;
          });
          // 把完整的数据保存到 list 中
          this.list = result.body.message;
        }
      });
    }
  },
  components: {
    // 注册 评论子组件
    "cmt-box": comment
  }
};
</script>

<style lang="scss" scoped>
.photoinfo-container {
  padding: 3px;
  h3 {
    color: #26a2ff;
    font-size: 15px;
    text-align: center;
    margin: 15px 0;
  }
  .subtitle {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
  }

  .content {
    font-size: 13px;
    line-height: 30px;
  }

  .thumbs{
    img{
      margin: 10px;
      box-shadow: 0 0 8px #999;
    }
  }
}
</style>

图片懒加载(muit提供)

<!-- 图片列表区域 -->
    <ul class="photo-list">
      <router-link v-for="item in list" :key="item.id" :to="'/home/photoinfo/' + item.id" tag="li">
        <img v-lazy="item.img_url">
        <div class="info">
          <h1 class="info-title">{{ item.title }}</h1>
          <div class="info-body">{{ item.zhaiyao }}</div>
        </div>
      </router-link>
    </ul>

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

相关文章:

验证码:
移动技术网