开始

封装自定义导航组件,除了关注的列表,还有话题页面。如果代码都写在这个页面里面的话,后期不方便管理。
image.png
新建news目录
image.png
image.png

image.png
注意最外层还是套这一个view
image.png

整个样式都复制过去
image.png

image.png

  1. /* .nav-left,.nav-right{
  2. border: 1upx solid;
  3. width: 100%;
  4. } */
  5. .nav-left {
  6. /* 这里视频中是左边16upx,实际这里是不需要的 */
  7. margin-left: 0upx;
  8. }
  9. .nav-left>view,
  10. .nav-right>view {
  11. font-size: 40upx;
  12. }
  13. .nav-left>view {
  14. color: #FF9619;
  15. }
  16. .nav-tab-bar {
  17. /* 父节点是flex为1的 所以这里100%宽度就可以继承父元素的宽度 */
  18. width: 100%;
  19. /* border: 1upx solid; */
  20. margin-left: -20upx;
  21. position: relative;
  22. }
  23. .nav-tab-bar>view {
  24. /* border: 1upx solid; */
  25. font-size: 32upx;
  26. padding: 0 15upx;
  27. font-weight: bold;
  28. color: #969696;
  29. }
  30. .active {
  31. color: #333333 !important;
  32. }
  33. .nav-tab-bar-line {
  34. border-bottom: 14upx solid #FEDE33;
  35. /* border-top: 7upx solid red; */
  36. width: 70upx;
  37. border-radius: 20upx;
  38. position: absolute;
  39. bottom: 12upx;
  40. }

方法也移植过去
image.png
两个方法都剪切过来
image.png

事件,发射
image.png

  1. methods:{
  2. changeTab(index) {
  3. this.$emit('change-tab',index);
  4. },
  5. openAdd() {
  6. // 打开发布页面
  7. uni.navigateTo({
  8. url: '../add-input/add-input'
  9. })
  10. }
  11. }

这个组件也要在子组件内引用
image.png

image.png

props属性定义
image.png

引入组件

image.png

  1. import newsNavBar from '@/components/news/news-nav-bar.vue';
  2. newsNavBar

image.png

  1. <news-nav-bar :tabBars="tabBars" :tabIndex="tabIndex"></news-nav-bar>

父组件。监听事件
image.png

  1. <news-nav-bar :tabBars="tabBars" :tabIndex="tabIndex"
  2. @change-tab="changeTab"></news-nav-bar>

父组件内事件的监听
image.png

  1. // 点击切换
  2. changeTab(index) {
  3. this.tabIndex = index;
  4. }

image.png

优化右下角图标

image.png

第四行的第二个view
image.png

image.png
再调大一点 28
image.png

  1. .common-list-r>view:nth-child(4)>view:nth-child(2)>view {
  2. margin-left: 10upx;
  3. padding-left: 5upx;
  4. font-size: 28upx;
  5. }

image.png
距离图片的间距,增加
image.png
image.png

  1. .common-list-r>view:nth-child(3) {
  2. position: relative;
  3. margin-bottom: 10upx;
  4. }

image.png

导航阴影

image.png

  1. <uni-nav-bar :fixed="true" :border="false" :statusBar="true" @clickRight="openAdd">

image.png

本节代码

news-nav-bar.vue

  1. <template>
  2. <view>
  3. <!-- 自定义导航栏 -->
  4. <uni-nav-bar :fixed="true" :border="false" :statusBar="true" @clickRight="openAdd">
  5. <!-- 左边 -->
  6. <block slot="left">
  7. <view class="nav-left">
  8. <view class="icon iconfont icon-qiandao"></view>
  9. </view>
  10. </block>
  11. <!-- 中间 -->
  12. <view class="nav-tab-bar u-f-ajc">
  13. <block v-for="(tab,index) in tabBars" :key="tab.id">
  14. <view class="u-f-ajc" :class="{'active':tabIndex==index}" @tap="changeTab(index)">
  15. {{tab.name}}
  16. <view v-if="(tabIndex==index)" class="nav-tab-bar-line"></view>
  17. </view>
  18. </block>
  19. </view>
  20. <!-- 右边 -->
  21. <block slot="right">
  22. <view class="nav-right u-f-ajc">
  23. <view class="icon iconfont icon-bianji1"></view>
  24. </view>
  25. </block>
  26. </uni-nav-bar>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. props: {
  32. tabBars: Array,
  33. tabIndex: Number
  34. },
  35. methods: {
  36. changeTab(index) {
  37. this.$emit('change-tab', index);
  38. },
  39. openAdd() {
  40. // 打开发布页面
  41. uni.navigateTo({
  42. url: '../add-input/add-input'
  43. })
  44. }
  45. }
  46. }
  47. </script>
  48. <style scoped>
  49. /* .nav-left,.nav-right{
  50. border: 1upx solid;
  51. width: 100%;
  52. } */
  53. .nav-left {
  54. /* 这里视频中是左边16upx,实际这里是不需要的 */
  55. margin-left: 0upx;
  56. }
  57. .nav-left>view,
  58. .nav-right>view {
  59. font-size: 40upx;
  60. }
  61. .nav-left>view {
  62. color: #FF9619;
  63. }
  64. .nav-tab-bar {
  65. /* 父节点是flex为1的 所以这里100%宽度就可以继承父元素的宽度 */
  66. width: 100%;
  67. /* border: 1upx solid; */
  68. margin-left: -20upx;
  69. position: relative;
  70. }
  71. .nav-tab-bar>view {
  72. /* border: 1upx solid; */
  73. font-size: 32upx;
  74. padding: 0 15upx;
  75. font-weight: bold;
  76. color: #969696;
  77. }
  78. .active {
  79. color: #333333 !important;
  80. }
  81. .nav-tab-bar-line {
  82. border-bottom: 14upx solid #FEDE33;
  83. /* border-top: 7upx solid red; */
  84. width: 70upx;
  85. border-radius: 20upx;
  86. position: absolute;
  87. bottom: 12upx;
  88. }
  89. </style>

common-list.vue修改45节课的问题

上节课最下面的黑线 自己没有做出来。
image.png
现在知道是哪里的原因了。 边框线加错了地方,。
应该把边框加载右侧的最外层的view上面
image.png

  1. .common-list-r {
  2. flex: 1;
  3. margin-left: 15upx;
  4. border-bottom: 1upx solid #EEEEEE;
  5. padding-bottom: 10upx;
  6. }

common-list.vue最终

  1. <template>
  2. <view class="common-list u-f animated fadeInLeft faster">
  3. <view class="common-list-l">
  4. <image :src="item.userpic" mode="widthFix" lazy-load="true"></image>
  5. </view>
  6. <view class="common-list-r">
  7. <view class="u-f-ac u-f-jsb">
  8. <view class="u-f-ac">
  9. {{item.username}}
  10. <view class="tag-sex icon iconfont icon-nan"
  11. :class="[item.sex==0?'icon-nan':'icon-nv']">{{item.age}}</view>
  12. </view>
  13. <view v-show="!isguanzhu" class="icon iconfont icon-zengjia" @tap="guanzhu">关注</view>
  14. </view>
  15. <view class="">{{item.title}}</view>
  16. <view class="u-f-ajc">
  17. <!-- 图片 -->
  18. <image v-if="item.titlepic" :src="item.titlepic" mode="widthFix" lazy-load="true">
  19. </image>
  20. <!-- 视频 -->
  21. <template v-if="item.video">
  22. <view class="common-list-play icon iconfont icon-bofang"></view>
  23. <view class="common-list-playinfo">
  24. {{item.video.looknum}} 次播放 {{item.video.long}}
  25. </view>
  26. </template>
  27. <!-- 分享 -->
  28. <view v-if="item.share" class="common-list-share u-f-ac">
  29. <image :src="item.share.titlepic" mode="widthFix" lazy-load="true">
  30. </image>
  31. <view class="">{{item.share.title}}</view>
  32. </view>
  33. </view>
  34. <view class="u-f-ajc u-f-jsb">
  35. <view class="">{{item.path}}</view>
  36. <view class="u-f-ac">
  37. <view class="icon iconfont icon-zhuanfa">
  38. {{item.sharenum}}
  39. </view>
  40. <view class="icon iconfont icon-pinglun1">
  41. {{item.commentnum}}
  42. </view>
  43. <view class="icon iconfont icon-dianzan1">
  44. {{item.goodnum}}
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. props:{
  54. item:Object,
  55. index:Number
  56. },
  57. data() {
  58. return {
  59. isguanzhu: this.item.isguanzhu
  60. }
  61. },
  62. methods:{
  63. guanzhu(){
  64. this.isguanzhu=true;
  65. uni.showToast({
  66. title: '关注成功',
  67. });
  68. }
  69. }
  70. }
  71. </script>
  72. <style scoped>
  73. .common-list {
  74. padding: 20upx;
  75. }
  76. .common-list-l {
  77. flex-shrink: 0;
  78. }
  79. .common-list-l image {
  80. width: 90upx;
  81. height: 90upx;
  82. border-radius: 100%;
  83. }
  84. .common-list-r {
  85. flex: 1;
  86. margin-left: 15upx;
  87. border-bottom: 1upx solid #EEEEEE;
  88. padding-bottom: 10upx;
  89. }
  90. .common-list-r>view:nth-child(3)>image {
  91. width: 100%;
  92. border-radius: 10upx;
  93. }
  94. .common-list-r>view:nth-child(1)>view:first-child {
  95. color: #999999;
  96. color: 30upx;
  97. }
  98. .common-list-r>view:nth-child(1)>view:last-child {
  99. background: #EEEEEEE;
  100. padding: 0 10upx;
  101. font-size: 32upx;
  102. }
  103. .tag-sex {
  104. background: #007AFF;
  105. color: #FFFFFF;
  106. font-size: 23upx;
  107. padding: 5upx 10upx;
  108. margin-left: 10upx;
  109. border-radius: 20upx;
  110. line-height: 22upx;
  111. }
  112. .common-list-r>view:nth-child(2) {
  113. font-size: 32upx;
  114. padding: 12upx 0;
  115. }
  116. .common-list-r>view:nth-child(3) {
  117. position: relative;
  118. margin-bottom: 10upx;
  119. }
  120. .common-list-play,
  121. .common-list-playinfo {
  122. position: absolute;
  123. color: #FFFFFF;
  124. }
  125. .common-list-play {
  126. font-size: 130upx;
  127. }
  128. .common-list-playinfo {
  129. right: 10upx;
  130. bottom: 10upx;
  131. background: rgba(51, 51, 51, 0.73);
  132. border-radius: 20upx;
  133. padding: 0 20upx;
  134. font-size: 26upx;
  135. }
  136. .common-list-r>view:nth-child(4)>view {
  137. color: #AAAAAA;
  138. }
  139. .common-list-r>view:nth-child(4)>view:nth-child(2)>view {
  140. margin-left: 10upx;
  141. padding-left: 5upx;
  142. font-size: 28upx;
  143. }
  144. .common-list-share {
  145. background: #EEEEEE;
  146. /* border: 1upx solid; */
  147. width: 100%;
  148. padding: 10upx;
  149. border-radius: 10upx;
  150. }
  151. .common-list-share>image {
  152. width: 200upx;
  153. height: 150upx;
  154. margin-right: 10upx;
  155. }
  156. </style>

结束