开始

封装在公共的文件夹下
image.png

image.png
block里面整个内容复制出去
image.png
css也复制过去
image.png
声明作用域
image.png

接收两个外部的属性
image.png

引入组件,使用组件

image.png

image.png

image.png

优化,增加动画特效

image.png

  1. <view class="common-list u-f animated fadeInLeft faster">

列表信息从右侧闪出来
image.png

封装组件

这里不知道是自己没注意看视频的内容,还是视频中确实没有讲这段。
需要中间的列表封装组件。
创建组件
image.png
整块的view内容复制过来
image.png
定义对象和索引这两个属性就可以了
image.png
.common-list相关的属性都复制过来。
image.png
引入组件。使用组件。
image.png

组件最终代码

  1. <template>
  2. <view>
  3. <!-- 自定义导航栏 -->
  4. <uni-nav-bar :fixed="true" :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 tabBaras" :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. <!-- 列表 -->
  28. <block v-for="(item,index) in list" :ket="index">
  29. <common-list :item="item" :index="index"></common-list>
  30. </block>
  31. </view>
  32. </template>
  33. <script>
  34. import commonList from '@/components/common/common-list.vue'
  35. export default {
  36. components:{
  37. commonList
  38. },
  39. data() {
  40. return {
  41. tabIndex: 0,
  42. tabBaras: [{
  43. name: '关注',
  44. id: 'guanzhu'
  45. },
  46. {
  47. name: '话题',
  48. id: 'toppic'
  49. },
  50. ],
  51. list: [
  52. // 文字
  53. {
  54. userpic: "../../static/demo/userpic/12.jpg",
  55. username: "哈哈",
  56. sex: 0, //0 男 1 女
  57. age: 25,
  58. isguanzhu: false,
  59. title: "我是标题",
  60. titlepic: "",
  61. video: false,
  62. share: false,
  63. path: "深圳 龙岗",
  64. sharenum: 20,
  65. commentnum: 30,
  66. goodnum: 20
  67. },
  68. // 图文
  69. {
  70. userpic: "../../static/demo/userpic/12.jpg",
  71. username: "哈哈",
  72. sex: 0, //0 男 1 女
  73. age: 25,
  74. isguanzhu: false,
  75. title: "我是标题",
  76. titlepic: "../../static/demo/datapic/13.jpg",
  77. video: false,
  78. share: false,
  79. path: "深圳 龙岗",
  80. sharenum: 20,
  81. commentnum: 30,
  82. goodnum: 20
  83. },
  84. // 视频
  85. {
  86. userpic: "../../static/demo/userpic/12.jpg",
  87. username: "哈哈",
  88. sex: 0, //0 男 1 女
  89. age: 25,
  90. isguanzhu: false,
  91. title: "我是标题",
  92. titlepic: "../../static/demo/datapic/13.jpg",
  93. video: {
  94. looknum: "20w",
  95. long: "2:47"
  96. },
  97. share: false,
  98. path: "深圳 龙岗",
  99. sharenum: 20,
  100. commentnum: 30,
  101. goodnum: 20
  102. },
  103. // 分享
  104. {
  105. userpic: "../../static/demo/userpic/12.jpg",
  106. username: "哈哈",
  107. sex: 0, //0 男 1 女
  108. age: 25,
  109. isguanzhu: false,
  110. title: "我是标题",
  111. titlepic: "",
  112. video: false,
  113. share: {
  114. title: "我是分享的标题",
  115. titlepic: "../../static/demo/datapic/14.jpg"
  116. },
  117. path: "深圳 龙岗",
  118. sharenum: 20,
  119. commentnum: 30,
  120. goodnum: 20
  121. },
  122. ]
  123. }
  124. },
  125. methods: {
  126. changeTab(index) {
  127. this.tabIndex = index;
  128. },
  129. openAdd() {
  130. uni.navigateTo({
  131. url: '../add-input/add-input'
  132. })
  133. }
  134. }
  135. }
  136. </script>
  137. <style>
  138. /* .nav-left,.nav-right{
  139. border: 1upx solid;
  140. width: 100%;
  141. } */
  142. .nav-left {
  143. /* 这里视频中是左边16upx,实际这里是不需要的 */
  144. margin-left: 0upx;
  145. }
  146. .nav-left>view,
  147. .nav-right>view {
  148. font-size: 40upx;
  149. }
  150. .nav-left>view {
  151. color: #FF9619;
  152. }
  153. .nav-tab-bar {
  154. /* 父节点是flex为1的 所以这里100%宽度就可以继承父元素的宽度 */
  155. width: 100%;
  156. /* border: 1upx solid; */
  157. margin-left: -20upx;
  158. position: relative;
  159. }
  160. .nav-tab-bar>view {
  161. /* border: 1upx solid; */
  162. font-size: 32upx;
  163. padding: 0 15upx;
  164. font-weight: bold;
  165. color: #969696;
  166. }
  167. .active {
  168. color: #333333 !important;
  169. }
  170. .nav-tab-bar-line {
  171. border-bottom: 14upx solid #FEDE33;
  172. /* border-top: 7upx solid red; */
  173. width: 70upx;
  174. border-radius: 20upx;
  175. position: absolute;
  176. bottom: 12upx;
  177. }
  178. </style>

关注按钮事件

image.png

  1. data() {
  2. return {
  3. isguanzhu: this.item.isguanzhu
  4. }
  5. },

image.png

  1. <view v-show="!isguanzhu" class="icon iconfont icon-zengjia" @tap="guanzhu">关注</view>

添加点击事件
image.png

image.png

  1. methods:{
  2. guanzhu(){
  3. this.isguanzhu=true;
  4. uni.showToast({
  5. title: '关注成功',
  6. });
  7. }
  8. }

点击关注后消失
image.png

提示

关注成功的提示信息
image.png

image.png

本节最终代码

common-list组件

  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: 10upx;
  87. }
  88. .common-list-r>view:nth-child(3)>image {
  89. width: 100%;
  90. border-radius: 10upx;
  91. }
  92. .common-list-r>view:nth-child(1)>view:first-child {
  93. color: #999999;
  94. color: 30upx;
  95. }
  96. .common-list-r>view:nth-child(1)>view:last-child {
  97. background: #EEEEEEE;
  98. padding: 0 10upx;
  99. font-size: 32upx;
  100. }
  101. .tag-sex {
  102. background: #007AFF;
  103. color: #FFFFFF;
  104. font-size: 23upx;
  105. padding: 5upx 10upx;
  106. margin-left: 10upx;
  107. border-radius: 20upx;
  108. line-height: 22upx;
  109. }
  110. .common-list-r>view:nth-child(2) {
  111. font-size: 32upx;
  112. padding: 12upx 0;
  113. }
  114. .common-list-r>view:nth-child(3) {
  115. position: relative;
  116. }
  117. .common-list-play,
  118. .common-list-playinfo {
  119. position: absolute;
  120. color: #FFFFFF;
  121. }
  122. .common-list-play {
  123. font-size: 130upx;
  124. }
  125. .common-list-playinfo {
  126. right: 10upx;
  127. bottom: 10upx;
  128. background: rgba(51, 51, 51, 0.73);
  129. border-radius: 20upx;
  130. padding: 0 20upx;
  131. font-size: 26upx;
  132. }
  133. .common-list-r>view:nth-child(4)>view {
  134. color: #AAAAAA;
  135. }
  136. .common-list-r>view:nth-child(4)>view:nth-child(2)>view {
  137. margin-left: 10upx;
  138. padding-left: 5upx;
  139. border-bottom: 1upx solid #EEEEEE;
  140. padding-bottom: 10upx;
  141. }
  142. .common-list-share {
  143. background: #EEEEEE;
  144. /* border: 1upx solid; */
  145. width: 100%;
  146. padding: 10upx;
  147. border-radius: 10upx;
  148. }
  149. .common-list-share>image {
  150. width: 200upx;
  151. height: 150upx;
  152. margin-right: 10upx;
  153. }
  154. </style>

news.vue页面

  1. <template>
  2. <view>
  3. <!-- 自定义导航栏 -->
  4. <uni-nav-bar :fixed="true" :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 tabBaras" :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. <!-- 列表 -->
  28. <block v-for="(item,index) in list" :ket="index">
  29. <common-list :item="item" :index="index"></common-list>
  30. </block>
  31. </view>
  32. </template>
  33. <script>
  34. import commonList from '@/components/common/common-list.vue'
  35. export default {
  36. components:{
  37. commonList
  38. },
  39. data() {
  40. return {
  41. tabIndex: 0,
  42. tabBaras: [{
  43. name: '关注',
  44. id: 'guanzhu'
  45. },
  46. {
  47. name: '话题',
  48. id: 'toppic'
  49. },
  50. ],
  51. list: [
  52. // 文字
  53. {
  54. userpic: "../../static/demo/userpic/12.jpg",
  55. username: "哈哈",
  56. sex: 0, //0 男 1 女
  57. age: 25,
  58. isguanzhu: false,
  59. title: "我是标题",
  60. titlepic: "",
  61. video: false,
  62. share: false,
  63. path: "深圳 龙岗",
  64. sharenum: 20,
  65. commentnum: 30,
  66. goodnum: 20
  67. },
  68. // 图文
  69. {
  70. userpic: "../../static/demo/userpic/12.jpg",
  71. username: "哈哈",
  72. sex: 0, //0 男 1 女
  73. age: 25,
  74. isguanzhu: false,
  75. title: "我是标题",
  76. titlepic: "../../static/demo/datapic/13.jpg",
  77. video: false,
  78. share: false,
  79. path: "深圳 龙岗",
  80. sharenum: 20,
  81. commentnum: 30,
  82. goodnum: 20
  83. },
  84. // 视频
  85. {
  86. userpic: "../../static/demo/userpic/12.jpg",
  87. username: "哈哈",
  88. sex: 0, //0 男 1 女
  89. age: 25,
  90. isguanzhu: false,
  91. title: "我是标题",
  92. titlepic: "../../static/demo/datapic/13.jpg",
  93. video: {
  94. looknum: "20w",
  95. long: "2:47"
  96. },
  97. share: false,
  98. path: "深圳 龙岗",
  99. sharenum: 20,
  100. commentnum: 30,
  101. goodnum: 20
  102. },
  103. // 分享
  104. {
  105. userpic: "../../static/demo/userpic/12.jpg",
  106. username: "哈哈",
  107. sex: 0, //0 男 1 女
  108. age: 25,
  109. isguanzhu: false,
  110. title: "我是标题",
  111. titlepic: "",
  112. video: false,
  113. share: {
  114. title: "我是分享的标题",
  115. titlepic: "../../static/demo/datapic/14.jpg"
  116. },
  117. path: "深圳 龙岗",
  118. sharenum: 20,
  119. commentnum: 30,
  120. goodnum: 20
  121. },
  122. ]
  123. }
  124. },
  125. methods: {
  126. changeTab(index) {
  127. this.tabIndex = index;
  128. },
  129. openAdd() {
  130. uni.navigateTo({
  131. url: '../add-input/add-input'
  132. })
  133. }
  134. }
  135. }
  136. </script>
  137. <style>
  138. /* .nav-left,.nav-right{
  139. border: 1upx solid;
  140. width: 100%;
  141. } */
  142. .nav-left {
  143. /* 这里视频中是左边16upx,实际这里是不需要的 */
  144. margin-left: 0upx;
  145. }
  146. .nav-left>view,
  147. .nav-right>view {
  148. font-size: 40upx;
  149. }
  150. .nav-left>view {
  151. color: #FF9619;
  152. }
  153. .nav-tab-bar {
  154. /* 父节点是flex为1的 所以这里100%宽度就可以继承父元素的宽度 */
  155. width: 100%;
  156. /* border: 1upx solid; */
  157. margin-left: -20upx;
  158. position: relative;
  159. }
  160. .nav-tab-bar>view {
  161. /* border: 1upx solid; */
  162. font-size: 32upx;
  163. padding: 0 15upx;
  164. font-weight: bold;
  165. color: #969696;
  166. }
  167. .active {
  168. color: #333333 !important;
  169. }
  170. .nav-tab-bar-line {
  171. border-bottom: 14upx solid #FEDE33;
  172. /* border-top: 7upx solid red; */
  173. width: 70upx;
  174. border-radius: 20upx;
  175. position: absolute;
  176. bottom: 12upx;
  177. }
  178. </style>

结束