开始

封装成组件
在components目录下新建paper目录,再在里面新建vue文件
image.png

image.png

初始化数据

image.png

  1. list:[
  2. {
  3. userpic:"../../static/demo/userpic/12.jpg",
  4. username:"昵称",
  5. time:"10:21",
  6. data:"我是信息",
  7. noreadnum:2
  8. },
  9. {
  10. userpic:"../../static/demo/userpic/12.jpg",
  11. username:"昵称",
  12. time:"10:21",
  13. data:"我是信息",
  14. noreadnum:0
  15. },
  16. {
  17. userpic:"../../static/demo/userpic/12.jpg",
  18. username:"昵称",
  19. time:"10:21",
  20. data:"我是信息",
  21. noreadnum:0
  22. },
  23. {
  24. userpic:"../../static/demo/userpic/12.jpg",
  25. username:"昵称",
  26. time:"10:21",
  27. data:"我是信息",
  28. noreadnum:11
  29. }
  30. ]

image.png

image.png
用template来显示 角标
image.png

  1. <!-- 小纸条列表 -->
  2. <block v-for="(item,index) in list" :key="index">
  3. <view class="paper-list u-f-ac">
  4. <image :src="item.userpic" mode="widthFix" lazy-load="true"></image>
  5. <view class="">
  6. <view class="u-f-ac u-f-jsb">
  7. {{item.username}}
  8. <view>{{item.time}}</view>
  9. </view>
  10. <view class="u-f-ac u-f-jsb">{{item.data}}
  11. <template v-if="item.noreadnum>0">
  12. <uni-badge :text="item.noreadnum" type="error"></uni-badge>
  13. </template>
  14. </view>
  15. </view>
  16. </view>
  17. </block>

多创建几个列表数据
image.png

image.png

封装 组件

image.png
paper-list开始的样式 往下都复制过来。
image.png

  1. <style scoped>
  2. .paper-list {
  3. border-bottom: 1upx solid #EEEEEE;
  4. padding: 20upx 0;
  5. }
  6. .paper-list>image {
  7. width: 100upx;
  8. height: 100upx;
  9. border-radius: 100%;
  10. margin-right: 20upx;
  11. flex-shrink: 0;
  12. }
  13. .paper-list>view {
  14. flex: 1;
  15. }
  16. .paper-list>view>view:first-child {
  17. font-size: 35upx;
  18. }
  19. .paper-list>view>view:first-child>view {
  20. color: #CBCBCB;
  21. }
  22. .paper-list>view>view:last-child {
  23. color: #999999;
  24. }
  25. </style>

image.png

  1. <script>
  2. export default{
  3. props:{
  4. item: Object,
  5. index: Number
  6. }
  7. }
  8. </script>

image.png

  1. import paperList from '@/components/paper/paper-list.vue';
  2. export default {
  3. components:{
  4. paperList
  5. },

image.png

  1. <block v-for="(item,index) in list" :key="index">
  2. <paper-list :item="item" :index="index"></paper-list>
  3. </block>

角标没有显示。
image.png
角标在组件
image.png

image.png

image.png

本节代码

paper-list.vue组件

  1. <template>
  2. <view class="paper-list u-f-ac">
  3. <image :src="item.userpic" mode="widthFix" lazy-load="true"></image>
  4. <view class="">
  5. <view class="u-f-ac u-f-jsb">
  6. {{item.username}}
  7. <view>{{item.time}}</view>
  8. </view>
  9. <view class="u-f-ac u-f-jsb">{{item.data}}
  10. <template v-if="item.noreadnum>0">
  11. <uni-badge :text="item.noreadnum" type="error"></uni-badge>
  12. </template>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default{
  19. props:{
  20. item: Object,
  21. index: Number
  22. }
  23. }
  24. </script>
  25. <style scoped>
  26. .paper-list {
  27. border-bottom: 1upx solid #EEEEEE;
  28. padding: 20upx 0;
  29. }
  30. .paper-list>image {
  31. width: 100upx;
  32. height: 100upx;
  33. border-radius: 100%;
  34. margin-right: 20upx;
  35. flex-shrink: 0;
  36. }
  37. .paper-list>view {
  38. flex: 1;
  39. }
  40. .paper-list>view>view:first-child {
  41. font-size: 35upx;
  42. }
  43. .paper-list>view>view:first-child>view {
  44. color: #CBCBCB;
  45. }
  46. .paper-list>view>view:last-child {
  47. color: #999999;
  48. }
  49. </style>

paper页面

  1. <template>
  2. <view class="body">
  3. <!-- 小纸条列表 -->
  4. <block v-for="(item,index) in list" :key="index">
  5. <paper-list :item="item" :index="index"></paper-list>
  6. </block>
  7. </view>
  8. </template>
  9. <script>
  10. import paperList from '@/components/paper/paper-list.vue';
  11. export default {
  12. components:{
  13. paperList
  14. },
  15. data() {
  16. return {
  17. list: [{
  18. userpic: "../../static/demo/userpic/12.jpg",
  19. username: "昵称",
  20. time: "10:21",
  21. data: "我是信息",
  22. noreadnum: 2
  23. },
  24. {
  25. userpic: "../../static/demo/userpic/12.jpg",
  26. username: "昵称",
  27. time: "10:21",
  28. data: "我是信息",
  29. noreadnum: 0
  30. },
  31. {
  32. userpic: "../../static/demo/userpic/12.jpg",
  33. username: "昵称",
  34. time: "10:21",
  35. data: "我是信息",
  36. noreadnum: 0
  37. },
  38. {
  39. userpic: "../../static/demo/userpic/12.jpg",
  40. username: "昵称",
  41. time: "10:21",
  42. data: "我是信息",
  43. noreadnum: 11
  44. }
  45. ]
  46. }
  47. },
  48. methods: {
  49. }
  50. }
  51. </script>
  52. <style>
  53. .body {
  54. padding: 0 20upx;
  55. }
  56. </style>

结束