开始

封装组件

已登录
image.png

这几块分别都可以封装成组件
image.png

封装组件-列表

image.png
首先来构建data数据
image.png

  1. list:[
  2. { icon:"liulan",name:"浏览历史" },
  3. { icon:"huiyuanvip",name:"糗百认证" },
  4. { icon:"keyboard",name:"审核糗事" },
  5. ]

循环
image.png

image.png

  1. <view class="home-list">
  2. <block v-for="(item,index) in list" :key="index">
  3. <view class="home-list-item u-f-ac u-f-jsb" hover-class="home-list-hover">
  4. <view class="u-f-ac">
  5. <view class="icon iconfont"
  6. :class="['icon-'+item.icon]"></view>
  7. {{item.name}}
  8. </view>
  9. <view class="icon iconfont icon-jinru"></view>
  10. </view>
  11. </block>
  12. </view>

image.png
改颜色
image.png

  1. .home-list-item {
  2. padding: 20upx;
  3. border-top: 1upx solid #F4F4F4;
  4. border-bottom: 1upx solid #F4F4F4;
  5. }

image.png

封装列表组件

先新建home目录,然后新建组件home-list-item组件。
image.png

image.png

  1. <view class="home-list-item u-f-ac u-f-jsb" hover-class="home-list-hover">
  2. <view class="u-f-ac">
  3. <view class="icon iconfont"
  4. :class="['icon-'+item.icon]"></view>
  5. {{item.name}}
  6. </view>
  7. <view class="icon iconfont icon-jinru"></view>
  8. </view>

image.png

  1. <style scoped>
  2. .home-list-item {
  3. padding: 20upx;
  4. border-top: 1upx solid #F4F4F4;
  5. border-bottom: 1upx solid #F4F4F4;
  6. }
  7. .home-list-item>view:first-child {
  8. color: #333333;
  9. }
  10. /* 左侧图标距离文字的间距 */
  11. .home-list-item>view:first-child>view {
  12. margin-right: 10upx;
  13. }
  14. /* 右边箭头 */
  15. .home-list-item>view:last-child {
  16. color: #CCCCCC;
  17. }
  18. .home-list-hover {
  19. background: #f4f4f4;
  20. }
  21. </style>

image.png

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

引用组件

image.png

  1. import homeListItem from '@/components/home/home-list-item.vue';

image.png

  1. <home-list-item :item="item" :index="index"></home-list-item>

封装登录后组件

image.png

构建数据
image.png

  1. homeinfo:{
  2. userpic:"../../static/demo/userpic/11.jpg",
  3. username:"昵称",
  4. totalnum:0,
  5. todaynum:0,
  6. },

image.png

  1. <view class="home-info u-f-ac">
  2. <image :src="homeinfo.userpic" mode="widthFix" lazy-load="true"></image>
  3. <view class="u-f1">
  4. <view>{{homeinfo.username}}</view>
  5. <view>总访客 {{homeinfo.totalnum}} 今日 {{homeinfo.todaynum}}</view>
  6. </view>
  7. <view class="icon iconfont icon-jinru"></view>
  8. </view>

封装组件

image.png

image.png
css
image.png

  1. <style scoped>
  2. /* 登陆后的 */
  3. .home-info {
  4. padding: 40upx;
  5. }
  6. .home-info>image {
  7. flex-shrink: 0;
  8. width: 100upx;
  9. height: 100upx;
  10. border-radius: 100%;
  11. margin-right: 15upx;
  12. }
  13. /* 最右侧的箭头 */
  14. .home-info>view:last-of-type {
  15. height: 100%;
  16. /* width: 70upx; */
  17. }
  18. /* 昵称 */
  19. .home-info>view:first-of-type>view:first-child {
  20. font-size: 32upx;
  21. }
  22. /* 总访客 */
  23. .home-info>view:first-of-type>view:last-child {
  24. color: #BBBBBB;
  25. }
  26. </style>

image.png

  1. export default{
  2. props:{
  3. homeinfo: Object
  4. }
  5. }

引用组件

image.png

  1. import homeInfo from '@/components/home/home-info.vue';

image.png

  1. <home-info :homeinfo="homeinfo"></home-info>

封装组件-第三方登陆

image.png

image.png

  1. <view class="other-login u-f-ac">
  2. <view class="u-f-ajc u-f1">
  3. <view class="icon iconfont icon-weixin u-f-ajc"></view>
  4. </view>
  5. <view class="u-f-ajc u-f1">
  6. <view class="icon iconfont icon-xinlangweibo u-f-ajc"></view>
  7. </view>
  8. <view class="u-f-ajc u-f1">
  9. <view class="icon iconfont icon-QQ u-f-ajc"></view>
  10. </view>
  11. </view>

css,

  1. .other-login {
  2. padding: 20upx 80upx;
  3. }
  4. .other-login>view>view {
  5. width: 100upx;
  6. height: 100upx;
  7. /* border: 1px solid; */
  8. border-radius: 100%;
  9. font-size: 55upx;
  10. color: #FFFFFF;
  11. }
  12. .other-login .icon-QQ {
  13. background: #2CAEFC;
  14. }
  15. .other-login .icon-weixin {
  16. background: #2BD19B;
  17. }
  18. .other-login .icon-xinlangweibo {
  19. background: #FC7729;
  20. }

不需要传任何值进来。
image.png

image.png

  1. <template v-else>
  2. <!-- 登陆 -->
  3. <home-info :homeinfo="homeinfo"></home-info>
  4. </template>

封装数据

image.png

image.png

  1. homedata: [{
  2. name: "糗事",
  3. num: 0
  4. },
  5. {
  6. name: "动态",
  7. num: 0
  8. },
  9. {
  10. name: "评论",
  11. num: 0
  12. },
  13. {
  14. name: "收藏",
  15. num: 0
  16. },
  17. ],

image.png

image.png

  1. <view class="home-data u-f-ac">
  2. <block v-for="(item,index) in homedata" :key="index">
  3. <view class="u-f1 u-f-ajc u-f-column">
  4. <view>{{item.num}}</view>{{item.name}}
  5. </view>
  6. </block>
  7. </view>

image.png

  1. <view class="home-data u-f-ac">
  2. <block v-for="(item,index) in homedata" :key="index">
  3. <view class="u-f1 u-f-ajc u-f-column">
  4. <view>{{item.num}}</view>{{item.name}}
  5. </view>
  6. </block>
  7. </view>

image.png

  1. <style scoped>
  2. .home-data {
  3. padding: 20upx 40upx;
  4. }
  5. .home-data>view {
  6. color: #989898;
  7. }
  8. .home-data>view>view {
  9. font-size: 32upx;
  10. color: #333333;
  11. }
  12. </style>

image.png

  1. <script>
  2. export default{
  3. props:{
  4. homedata: Array
  5. }
  6. }
  7. </script>

image.png

  1. import homeData from '@/components/home/home-data.vue';
  2. homeData

image.png

  1. <!-- 4个数据 -->
  2. <home-data :homedata="homedata"></home-data>

测试

image.png

改成未登陆
image.png

image.png

本节代码

home-list-item.vue组件

  1. <template>
  2. <view class="home-list-item u-f-ac u-f-jsb" hover-class="home-list-hover">
  3. <view class="u-f-ac">
  4. <view class="icon iconfont"
  5. :class="['icon-'+item.icon]"></view>
  6. {{item.name}}
  7. </view>
  8. <view class="icon iconfont icon-jinru"></view>
  9. </view>
  10. </template>
  11. <script>
  12. export default{
  13. props:{
  14. item: Object,
  15. index: Number
  16. }
  17. }
  18. </script>
  19. <style scoped>
  20. .home-list-item {
  21. padding: 20upx;
  22. border-top: 1upx solid #F4F4F4;
  23. border-bottom: 1upx solid #F4F4F4;
  24. }
  25. .home-list-item>view:first-child {
  26. color: #333333;
  27. }
  28. /* 左侧图标距离文字的间距 */
  29. .home-list-item>view:first-child>view {
  30. margin-right: 10upx;
  31. }
  32. /* 右边箭头 */
  33. .home-list-item>view:last-child {
  34. color: #CCCCCC;
  35. }
  36. .home-list-hover {
  37. background: #f4f4f4;
  38. }
  39. </style>

home-info.vue组件

  1. <template>
  2. <view class="home-info u-f-ac">
  3. <image :src="homeinfo.userpic" mode="widthFix" lazy-load="true"></image>
  4. <view class="u-f1">
  5. <view>{{homeinfo.username}}</view>
  6. <view>总访客 {{homeinfo.totalnum}} 今日 {{homeinfo.todaynum}}</view>
  7. </view>
  8. <view class="icon iconfont icon-jinru"></view>
  9. </view>
  10. </template>
  11. <script>
  12. export default{
  13. props:{
  14. homeinfo: Object
  15. }
  16. }
  17. </script>
  18. <style scoped>
  19. /* 登陆后的 */
  20. .home-info {
  21. padding: 40upx;
  22. }
  23. .home-info>image {
  24. flex-shrink: 0;
  25. width: 100upx;
  26. height: 100upx;
  27. border-radius: 100%;
  28. margin-right: 15upx;
  29. }
  30. /* 最右侧的箭头 */
  31. .home-info>view:last-of-type {
  32. height: 100%;
  33. /* width: 70upx; */
  34. }
  35. /* 昵称 */
  36. .home-info>view:first-of-type>view:first-child {
  37. font-size: 32upx;
  38. }
  39. /* 总访客 */
  40. .home-info>view:first-of-type>view:last-child {
  41. color: #BBBBBB;
  42. }
  43. </style>

other-login.vue组件

home-data.vue组价

  1. <template>
  2. <view class="home-data u-f-ac">
  3. <block v-for="(item,index) in homedata" :key="index">
  4. <view class="u-f1 u-f-ajc u-f-column">
  5. <view>{{item.num}}</view>{{item.name}}
  6. </view>
  7. </block>
  8. </view>
  9. </template>
  10. <script>
  11. export default{
  12. props:{
  13. homedata: Array
  14. }
  15. }
  16. </script>
  17. <style scoped>
  18. .home-data {
  19. padding: 20upx 40upx;
  20. }
  21. .home-data>view {
  22. color: #989898;
  23. }
  24. .home-data>view>view {
  25. font-size: 32upx;
  26. color: #333333;
  27. }
  28. </style>

home.vue页面

  1. <template>
  2. <view>
  3. <template v-if="!islogin">
  4. <!-- 未登陆 -->
  5. <view class="u-f-ajc">登陆仿糗百,体验更多功能</view>
  6. <!-- 第三方登陆 -->
  7. <other-login></other-login>
  8. <!-- 账号密码登陆 -->
  9. <view class="u-f-ajc">
  10. 账号密码登陆<view class="icon iconfont icon-jinru"></view>
  11. </view>
  12. </template>
  13. <template v-else>
  14. <!-- 登陆 -->
  15. <home-info :homeinfo="homeinfo"></home-info>
  16. </template>
  17. <!-- 4个数据 -->
  18. <home-data :homedata="homedata"></home-data>
  19. <!-- 广告位 -->
  20. <view class="home-adv u-f-ajc">
  21. <image src="../../static/demo/demo20.jpg" mode="widthFix" lazy-load="true"></image>
  22. </view>
  23. <!-- 功能列表 -->
  24. <view class="home-list">
  25. <block v-for="(item,index) in list" :key="index">
  26. <home-list-item :item="item" :index="index"></home-list-item>
  27. </block>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import homeListItem from '@/components/home/home-list-item.vue';
  33. import homeInfo from '@/components/home/home-info.vue';
  34. import otherLogin from '@/components/home/other-login.vue';
  35. import homeData from '@/components/home/home-data.vue';
  36. export default {
  37. components: {
  38. homeListItem,
  39. homeInfo,
  40. otherLogin,
  41. homeData
  42. },
  43. data() {
  44. return {
  45. islogin: false,
  46. homeinfo: {
  47. userpic: "../../static/demo/userpic/11.jpg",
  48. username: "昵称",
  49. totalnum: 0,
  50. todaynum: 0,
  51. },
  52. homedata: [{
  53. name: "糗事",
  54. num: 0
  55. },
  56. {
  57. name: "动态",
  58. num: 0
  59. },
  60. {
  61. name: "评论",
  62. num: 0
  63. },
  64. {
  65. name: "收藏",
  66. num: 0
  67. },
  68. ],
  69. list: [{
  70. icon: "liulan",
  71. name: "浏览历史"
  72. },
  73. {
  74. icon: "huiyuanvip",
  75. name: "糗百认证"
  76. },
  77. {
  78. icon: "keyboard",
  79. name: "审核糗事"
  80. },
  81. ]
  82. }
  83. },
  84. onNavigationBarButtonTap(e) {
  85. if (e.index == 0) {
  86. console.log('跳转设置页');
  87. }
  88. },
  89. methods: {
  90. }
  91. }
  92. </script>
  93. <style>
  94. .home-list {
  95. padding: 20upx;
  96. }
  97. .home-adv {
  98. padding: 20upx;
  99. }
  100. .home-adv>image {
  101. border-radius: 20upx;
  102. height: 150upx;
  103. }
  104. </style>

结束