vue自己做的组件(仿评论区)

该文章展示了一个使用Vue.js编写的组件,用于创建一个两列布局的讨论模块。组件具有响应式设计,根据窗口大小动态调整各部分的尺寸,包括头像、用户名和内容区域的高度和宽度。同时,文章提供了入口文件homeWL.vue的示例,显示如何传入参数并使用该组件。
摘要由CSDN通过智能技术生成

1,效果
在这里插入图片描述
2,源码
链接:https://pan.baidu.com/s/1UM4vJv9wCBnB2PaWGIxf8Q?pwd=qkaa
提取码:qkaa
–来自百度网盘超级会员V4的分享
3,代码
目录
在这里插入图片描述
discuss.vue

<template>
<div class="wrapper" :style="{'--onepartheight':onepartheight,'--onepartwidth':onepartwidth,'--twopartheight':twopartheight,
'--twopartwidth':twopartwidth,'--twopartofoneheight':twopartofoneheight,'--twopartoftwoheight':twopartoftwoheight}">
  <div class="onepart">
    <el-avatar :src="url" :size="imgwidth" fit="fit">
    </el-avatar>
  </div>

  <div class="twopart">
    <div class="twopartofone">
      <span style="color: gray">{{name}}</span>
    </div>

    <div class="twopartoftwo">
      <p class="twopartfont">
        {{contentstxt}}
      </p>
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: "discussWL",
  data(){
    return{
      // //网名
      // name:'',
      // //内容
      // contentstxt:'',
      // //图片
      // url:'https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6a9119f10a524c178207a4a6df500c00~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp?',
      // //图片的width
      // imgwidth:'',
      // //第一列的height度
      // onepartheight:'',
      // //第一列的width度
      // onepartwidth:'',
      // //第二列的height度
      // twopartheight:'',
      // //第二列的width度
      // twopartwidth:'',
      // //第二列的第一行的height
      // twopartofoneheight:'',
      // //第二列的第二行的height
      // twopartoftwoheight:''
    }
  },
  props:['name','contentstxt','url','imgwidth','onepartheight','onepartwidth','twopartheight','twopartwidth','twopartofoneheight','twopartoftwoheight',],

}
</script>

<style scoped>
:root{
/*第一列的height度*/
  --onepartheight:'';
/*  第一列的width度*/
  --onepartwidth: '';
/*  第二列的height度*/
  --twopartheight:'';
/*  第二列的width度*/
  --twopartwidth:'';
/*  第二列的第一行的height*/
  --twopartofoneheight:'';
/*  第二列的第二行的height*/
  --twopartoftwoheight:'' ;
}
.wrapper {

  margin: 30px;
  display: grid;
  grid-template-columns: var(--onepartwidth) var(--twopartwidth);
  grid-gap: 5px;
  grid-template-rows: var(--onepartheight);
}

.onepart{
  padding: 10px;

}
.twopart{
  padding: 20px 0 0  0;
  display: grid;

  grid-template-rows: var(--twopartofoneheight) var(--twopartoftwoheight);

}
.twopartofone{

}
.twopartoftwo{

}
.twopartfont{
  font-size: 50%;
}
</style>

入口文件homeWL.vue

<template>
<div>
  <discuss-w-l :name="name" :contentstxt="contentstxt" :imgwidth="imgwidth" :onepartheight="onepartheight" :onepartwidth="onepartwidth" :twopartheight="twopartheight"
  :twopartofoneheight="twopartofoneheight" :twopartoftwoheight="twopartoftwoheight" :twopartwidth="twopartwidth" :url="url"></discuss-w-l>
</div>
</template>

<script>
import DiscussWL from "@/components/discuss/discuss";
export default {
  name: "homeWL",
  data(){
    return{
      //网名
      name:'wl',
      //内容
      contentstxt:'暗恋是一种美好而含蓄的情感,像一阵微风,悄悄地轻拂心间。在内心深处嘀咕着对那个人的好感,却不敢表露出来。暗恋,让人纯洁而又脆弱,它是一种美好的情感,无需任何呼喊,却能给人带来独有的感觉。',
      //图片
      url:'https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6a9119f10a524c178207a4a6df500c00~tplv-k3u1fbpfcp-zoom-in-crop-mark:1512:0:0:0.awebp?',
      //图片的width
      imgwidth:'',
      //第一列的height度
      onepartheight:'',
      //第一列的width度
      onepartwidth:'',
      //第二列的height度
      twopartheight:'',
      //第二列的width度
      twopartwidth:'',
      //第二列的第一行的height
      twopartofoneheight:'',
      //第二列的第二行的height
      twopartoftwoheight:''
    }
  },
  mounted() {

    this.getWindowHeight()
    this.getWindowWidth()
    window.addEventListener('resize', this.getWindowHeight); // 在窗口大小改变时重新获取窗口宽度
    window.addEventListener('resize', this.getWindowWidth);
  },

  methods: {
    //获得浏览器窗口的高度
    getWindowHeight() {
      var windowHeight = window.innerHeight;

      this.onepartheight = parseInt(windowHeight/5)+'px'
      //第二列的第一行的height
      this.twopartofoneheight = parseInt(windowHeight/5/7)+'px'
      //第二列的第二行的height
      this.twopartoftwoheight = parseInt(windowHeight/5/2+20)+'px'
    },
    // 获得浏览器窗口的宽度
    getWindowWidth(){
      var windowWidth = window.innerWidth;

      // 图片的width
      this.imgwidth = parseInt(windowWidth/35)
      //第一列的width
      this.onepartwidth = parseInt(windowWidth/29)+'px'
      //第二列的width
      this.twopartwidth = parseInt(windowWidth/8)+'px'
    }
  },
  components: {DiscussWL}
}
</script>

<style scoped>

</style>

参数说明
在这里插入图片描述
需要调参数的话,直接在methods中的两个函数中调即可,直接调百分比即可。
name 网名
contentstxt 评论区的文本内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是梦磊OL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值