uni-app初探之天气预报小例子

前端之家收集整理的这篇文章主要介绍了uni-app初探之天气预报小例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

概述@H_403_2@

在实际工作中,App既需要支持Android手机,又要支持IOS手机,还要支持微信小程序,公众号等,面对这样的需求,是否劳心费力,苦不堪言,还要考虑各平台的兼容性。现在uni-app以“开发一次,多端覆盖”的理念,海纳百川,求同存异,受到大多数开发者的青睐。uni-app是采用vue.js作为开发前端应用的框架,开发者编写一套代码,可发布到iOS、Android、Web(响应式)、以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉/淘宝)、快应用等多个平台。本文以一个天气预报的小例子,简述uni-app的开发步骤。

为什么选择uni-app ?@H_403_2@

  1. uni-app实现了一套代码,同时运行到多个平台。
  2. uni-app在开发者数量、案例、跨端抹平度、扩展灵活性、性能体验、周边生态、学习成本、开发成本等8大关键指标上拥有更强的优势。
  3. DCloud为uni-app开发提供了开发利器HBuilderX,以其轻巧极速,强大的语法提示,清爽护眼,和专为vue量身打造的优势,吸引了大多数的开发者。

uni-app目录结构@H_403_2@

一个uni-app工程,默认包含如下目录及文件,如下图所示:

 

uni-app应用生命周期@H_403_2@

uni-app 支持如下应用生命周期函数

 

 注意:应用生命周期仅可在App.vue中监听,在其它页面监听无效。

uni-app页面生命周期@H_403_2@

uni-app 支持如下页面生命周期函数

 

 关于其他uni-app介绍,可以参考uni-app官网

示例效果图@H_403_2@

 本次开发是一个天气预报的小例子,在Chrome浏览器里面如下图所示:

 在Android手机上如下所示:

源码分析@H_403_2@

 在uni-app开发小例子时,为了代码的复用,自定义三个组件,包括,风车组件,圆形进度球组件,天气组件。源码分别如下:

(一)圆形进度球组件@H_403_2@

组件包含三部分,分别是页面(template),脚本(JavaScript),样式(CSS),源码如下:

页面(template)代码如下:

 1@H_403_2@ <@H_403_2@template@H_403_2@>@H_403_2@
 2@H_403_2@     view @H_403_2@class@H_403_2@="content"@H_403_2@ 3@H_403_2@         ="progress"@H_403_2@ 4@H_403_2@             ="progress_outer"@H_403_2@ 5@H_403_2@                 ="progress_inner"@H_403_2@></@H_403_2@view@H_403_2@ 6@H_403_2@                 ="progress_masker"@H_403_2@ :style@H_403_2@="maskStyle"@H_403_2@ 7@H_403_2@             </@H_403_2@ 8@H_403_2@             ="progress_value"@H_403_2@>@H_403_2@{{cvalue}}% 9@H_403_2@         10@H_403_2@     11@H_403_2@ >@H_403_2@
View Code@H_403_2@

脚本(JavaScript)代码如下:

 1@H_403_2@ <script>
 2@H_403_2@     export default@H_403_2@ {
@H_403_2@ 3@H_403_2@         props: {
@H_403_2@ 4@H_403_2@             cvalue: {
@H_403_2@//@H_403_2@ 进度条百分比@H_403_2@
 6@H_403_2@                 type: Number,@H_403_2@ 7@H_403_2@                 default@H_403_2@: 10,1)"> 8@H_403_2@             },1)"> 9@H_403_2@         },1)">10@H_403_2@ 
        data() {
@H_403_2@12@H_403_2@             return@H_403_2@13@H_403_2@ 
14@H_403_2@             };
@H_403_2@15@H_403_2@ 16@H_403_2@         computed: {
@H_403_2@17@H_403_2@             maskStyle(){
@H_403_2@18@H_403_2@                 var@H_403_2@ top=100-this@H_403_2@.cvalue;
@H_403_2@19@H_403_2@                 return@H_403_2@ {marginTop :  top + '%'};
@H_403_2@20@H_403_2@ 21@H_403_2@             
22@H_403_2@         }
@H_403_2@23@H_403_2@ 
24@H_403_2@     }
@H_403_2@25@H_403_2@ </script>
View Code@H_403_2@

样式(CSS)代码如下:

 1@H_403_2@ <style>
@H_403_2@ 2@H_403_2@     .content@H_403_2@{
 3@H_403_2@         width@H_403_2@: 200rpx@H_403_2@;
 4@H_403_2@         height@H_403_2@: 5@H_403_2@         display@H_403_2@: block@H_403_2@;
 6@H_403_2@         Box-sizing@H_403_2@: border-Box@H_403_2@;
 7@H_403_2@     }
 8@H_403_2@     .progress @H_403_2@{
 9@H_403_2@         position@H_403_2@: relative@H_403_2@;
10@H_403_2@ 11@H_403_2@ 12@H_403_2@         padding@H_403_2@: 0@H_403_2@;
13@H_403_2@ 14@H_403_2@     }
15@H_403_2@     .progress_outer
@H_403_2@16@H_403_2@     {
17@H_403_2@          height@H_403_2@:100%@H_403_2@;
18@H_403_2@          width@H_403_2@:19@H_403_2@          background-color@H_403_2@:gray@H_403_2@;
20@H_403_2@          border-radius@H_403_2@:calc(100%/2)@H_403_2@;
21@H_403_2@          border@H_403_2@:5px solid rgba(0,0.1)@H_403_2@;
22@H_403_2@          padding@H_403_2@:0@H_403_2@;
23@H_403_2@          Box-shadow@H_403_2@: 0px 2px 4px #555555@H_403_2@;
24@H_403_2@          -webkit-Box-shadow@H_403_2@:25@H_403_2@          -moz-Box-shadow@H_403_2@:26@H_403_2@          position@H_403_2@: absolute@H_403_2@;
27@H_403_2@          Box-sizing@H_403_2@:28@H_403_2@          overflow@H_403_2@: hidden@H_403_2@;
29@H_403_2@          
30@H_403_2@      }
31@H_403_2@       .progress_inner
@H_403_2@32@H_403_2@       {
33@H_403_2@           height@H_403_2@:34@H_403_2@           width@H_403_2@:35@H_403_2@           border@H_403_2@:1px solid yellow@H_403_2@;
36@H_403_2@           border-radius@H_403_2@:37@H_403_2@           position@H_403_2@:absolute@H_403_2@;
38@H_403_2@           background-color@H_403_2@:white@H_403_2@;
39@H_403_2@           text-align@H_403_2@:center@H_403_2@;
40@H_403_2@           Box-sizing@H_403_2@:41@H_403_2@           
42@H_403_2@       }
43@H_403_2@      .progress_masker
@H_403_2@44@H_403_2@      {
45@H_403_2@ 46@H_403_2@ 47@H_403_2@          background@H_403_2@: -webkit-gradient(linear,center top,center bottom,from(#0ff),to(#0f0))@H_403_2@;
48@H_403_2@  -moz-linear-gradient( top,#fff,#0f0)@H_403_2@;
49@H_403_2@  -o-linear-gradient( top,1)">50@H_403_2@ 51@H_403_2@ 52@H_403_2@      }
53@H_403_2@      .progress_value
@H_403_2@54@H_403_2@      {
55@H_403_2@ 56@H_403_2@          color@H_403_2@:black@H_403_2@;
57@H_403_2@          font-weight@H_403_2@:bolder@H_403_2@;
58@H_403_2@ transparent@H_403_2@;
59@H_403_2@          text-align@H_403_2@:60@H_403_2@ 61@H_403_2@          margin-top@H_403_2@: 90rpx@H_403_2@;
62@H_403_2@      }
63@H_403_2@ </style>@H_403_2@
View Code@H_403_2@

(二)风车组件@H_403_2@

风车组件包含两部分,分别是页面(template),样式(CSS),源码如下:

页面(template)代码如下:

="wind_mill"@H_403_2@="cicle"@H_403_2@ 5@H_403_2@             ="vane"@H_403_2@="vane1"@H_403_2@="vane2"@H_403_2@ 8@H_403_2@                 ="vane3"@H_403_2@ 9@H_403_2@             10@H_403_2@             ="blade"@H_403_2@11@H_403_2@                 
13@H_403_2@         14@H_403_2@     >@H_403_2@
View Code@H_403_2@

 样式(CSS)代码如下:

    .wind_mill@H_403_2@{
 220rpx@H_403_2@;
 5@H_403_2@         /*@H_403_2@ background-color:  rgba(25,83,157,0.5); @H_403_2@*/@H_403_2@
    @keyframes vanflash@H_403_2@{
        from{transform@H_403_2@: rotate(0deg)@H_403_2@; transform-origin@H_403_2@: center@H_403_2@;}
10@H_403_2@         to@H_403_2@{transform@H_403_2@: rotate(360deg)@H_403_2@;transform-origin@H_403_2@:11@H_403_2@ 12@H_403_2@     .vane@H_403_2@{
14@H_403_2@ 15@H_403_2@ 16@H_403_2@         animation-name@H_403_2@: vanflash@H_403_2@;
        animation-duration@H_403_2@: 5s@H_403_2@;
        animation-iteration-count@H_403_2@: infinite@H_403_2@;
        -webkit-animation-name@H_403_2@:        -webkit-animation-duration@H_403_2@:        -webkit-animation-iteration-count@H_403_2@:22@H_403_2@     }
23@H_403_2@     .vane1@H_403_2@{
 80rpx@H_403_2@;
 4rpx@H_403_2@;
        background-color@H_403_2@: #FFFFFF@H_403_2@;
        border-radius@H_403_2@: 5rpx@H_403_2@;
29@H_403_2@ 30@H_403_2@         left@H_403_2@: 100rpx@H_403_2@;
31@H_403_2@         top@H_403_2@:100rpx@H_403_2@;
32@H_403_2@         transform@H_403_2@: rotate(0deg)@H_403_2@;
        transform-origin@H_403_2@:left@H_403_2@;
        -webkit-transform@H_403_2@:rotate(0deg)@H_403_2@;
35@H_403_2@         
36@H_403_2@     }
37@H_403_2@     .vane2@H_403_2@{
41@H_403_2@ 42@H_403_2@ 43@H_403_2@ 44@H_403_2@  rotate(120deg)@H_403_2@;
rotate(120deg)@H_403_2@;
48@H_403_2@     }
49@H_403_2@     .vane3@H_403_2@{
52@H_403_2@ 53@H_403_2@ 54@H_403_2@  rotate(240deg)@H_403_2@;
rotate(240deg)@H_403_2@;
60@H_403_2@     }
61@H_403_2@     .cicle@H_403_2@{
62@H_403_2@ 63@H_403_2@ 64@H_403_2@ 90rpx@H_403_2@;
65@H_403_2@ 66@H_403_2@  20rpx@H_403_2@;
67@H_403_2@ 68@H_403_2@  16rpx@H_403_2@;
69@H_403_2@     }
70@H_403_2@     .blade@H_403_2@{
71@H_403_2@  120rpx@H_403_2@;
72@H_403_2@  10rpx@H_403_2@;
73@H_403_2@ 74@H_403_2@ 75@H_403_2@ 76@H_403_2@ 77@H_403_2@ 78@H_403_2@  rotate(90deg)@H_403_2@;
79@H_403_2@ 80@H_403_2@ rotate(90deg)@H_403_2@;
81@H_403_2@     }
82@H_403_2@ (三)天气组件@H_403_2@

天气组件,引用了前面两个组件,并有自定义数据内容,如下所示:

页面(template)代码如下:

scroll-view @H_403_2@scroll-y@H_403_2@="true"@H_403_2@ class@H_403_2@="main"@H_403_2@="current"@H_403_2@="district"@H_403_2@>@H_403_2@{{district}}="temp"@H_403_2@>@H_403_2@{{temp}}°C 6@H_403_2@             ="temp_range"@H_403_2@>@H_403_2@{{temprange}}="temp_desc"@H_403_2@>@H_403_2@{{tempdesc}}br@H_403_2@="temp_src"@H_403_2@10@H_403_2@                 ="temp_src_left"@H_403_2@>@H_403_2@中国气象11@H_403_2@                 ="temp_src_right"@H_403_2@>@H_403_2@上次更新时间:{{updatetime}}14@H_403_2@         scroll-x@H_403_2@="true"@H_403_2@15@H_403_2@             ="hour"@H_403_2@ enable-flex@H_403_2@16@H_403_2@                 ="each_hour"@H_403_2@ v-for@H_403_2@="item in timelist"@H_403_2@17@H_403_2@                     ="each_hour_time"@H_403_2@>@H_403_2@{{item.time}}18@H_403_2@                     image @H_403_2@:src@H_403_2@="item.img"@H_403_2@ mode@H_403_2@="scaleToFill"@H_403_2@="each_hour_img"@H_403_2@ image@H_403_2@19@H_403_2@                     ="each_hour_temp"@H_403_2@>@H_403_2@{{item.temp}}20@H_403_2@                 21@H_403_2@             22@H_403_2@         scroll-view@H_403_2@24@H_403_2@         ="sevenday"@H_403_2@25@H_403_2@             ="each_day"@H_403_2@="item in daylist"@H_403_2@26@H_403_2@                 ="each_day_text"@H_403_2@27@H_403_2@                     {{item.day}} {{item.week}}
@H_403_2@28@H_403_2@                 29@H_403_2@                 ="each_day_img"@H_403_2@ :src@H_403_2@=""@H_403_2@30@H_403_2@                 ="each_day_temp"@H_403_2@31@H_403_2@             32@H_403_2@ 
33@H_403_2@         34@H_403_2@         ="air"@H_403_2@35@H_403_2@             ="air_title"@H_403_2@36@H_403_2@                 =""@H_403_2@ style@H_403_2@="flex: 1;"@H_403_2@>@H_403_2@空气质量37@H_403_2@                 ="text-align: right;flex: 1;"@H_403_2@>@H_403_2@更多>38@H_403_2@             39@H_403_2@             ="air_body"@H_403_2@40@H_403_2@                 ="air_left"@H_403_2@41@H_403_2@                     airprogress @H_403_2@="airprogress"@H_403_2@ :cvalue@H_403_2@="airvalue"@H_403_2@airprogress@H_403_2@42@H_403_2@                 43@H_403_2@                 ="air_right"@H_403_2@44@H_403_2@                     ="air_content"@H_403_2@="item in airlist"@H_403_2@45@H_403_2@                         ="air_content_name"@H_403_2@>@H_403_2@{{item.name}}46@H_403_2@                         ="air_content_value"@H_403_2@>@H_403_2@{{item.value}}47@H_403_2@                     48@H_403_2@                 49@H_403_2@             50@H_403_2@         51@H_403_2@         ="wind"@H_403_2@52@H_403_2@             ="wind_title"@H_403_2@53@H_403_2@                 >@H_403_2@风向风力54@H_403_2@                 55@H_403_2@             56@H_403_2@             ="wind_body"@H_403_2@57@H_403_2@                 ="wind_left"@H_403_2@58@H_403_2@                     windmill @H_403_2@="wind01"@H_403_2@windmill@H_403_2@59@H_403_2@                     ="wind02"@H_403_2@60@H_403_2@                 61@H_403_2@                 ="wind_right"@H_403_2@62@H_403_2@                     ="wind_right_direction"@H_403_2@63@H_403_2@                         style@H_403_2@="flex: 1;text-align: left;"@H_403_2@>@H_403_2@风向64@H_403_2@                         >@H_403_2@{{winddirection}}65@H_403_2@                     66@H_403_2@                     ="wind_right_power"@H_403_2@67@H_403_2@                         >@H_403_2@风力68@H_403_2@                         >@H_403_2@{{windpower}}69@H_403_2@                     70@H_403_2@                 71@H_403_2@             72@H_403_2@         73@H_403_2@         ="provider"@H_403_2@>@H_403_2@provide by Alan.hsiang74@H_403_2@     75@H_403_2@ >@H_403_2@
View Code@H_403_2@

脚本(JavaScript)代码如下:

 2@H_403_2@     import windmill from "../windmill/windmill.vue"
 3@H_403_2@     import airprogress from "../airprogress/airprogress.vue"
 4@H_403_2@     export  5@H_403_2@         components: {
@H_403_2@            windmill,1)"> 7@H_403_2@             airprogress
@H_403_2@        props:{
@H_403_2@10@H_403_2@             district:{
@H_403_2@                type:String,1)">12@H_403_2@                 required:true@H_403_2@13@H_403_2@             temp:{
@H_403_2@                type:Number,1)">default@H_403_2@:0
18@H_403_2@             temprange:{
@H_403_2@19@H_403_2@ 20@H_403_2@                 
21@H_403_2@             tempdesc:{
@H_403_2@23@H_403_2@ 24@H_403_2@                 
25@H_403_2@ 26@H_403_2@             updatetime:{
@H_403_2@28@H_403_2@                 
29@H_403_2@ 30@H_403_2@             timelist:{
@H_403_2@31@H_403_2@                 type:Array,1)">32@H_403_2@                 
33@H_403_2@ 34@H_403_2@             daylist:{
@H_403_2@35@H_403_2@ 36@H_403_2@                 
37@H_403_2@ 38@H_403_2@             airvalue:{
@H_403_2@39@H_403_2@ default@H_403_2@:1041@H_403_2@                 
42@H_403_2@ 43@H_403_2@             airlist:{
@H_403_2@44@H_403_2@ 45@H_403_2@                 
46@H_403_2@ 47@H_403_2@             winddirection:{
@H_403_2@48@H_403_2@ 49@H_403_2@                 
50@H_403_2@ 51@H_403_2@             windpower:{
@H_403_2@52@H_403_2@ 53@H_403_2@                 
54@H_403_2@             }
@H_403_2@55@H_403_2@ 56@H_403_2@ 57@H_403_2@             58@H_403_2@                 
59@H_403_2@ 
60@H_403_2@ 61@H_403_2@ 62@H_403_2@ 63@H_403_2@ </script>
View Code@H_403_2@

样式(CSS)代码如下:

  1@H_403_2@   2@H_403_2@     view @H_403_2@{
  3@H_403_2@          border: #007AFF 1rpx solid; @H_403_2@  4@H_403_2@         font-family@H_403_2@: Arial,Helvetica,sans-serif@H_403_2@;
  5@H_403_2@         font-size@H_403_2@: 28rpx@H_403_2@;
  6@H_403_2@  2rpx@H_403_2@;
  7@H_403_2@     }
  8@H_403_2@ 
  9@H_403_2@     .main @H_403_2@{
 10@H_403_2@  100%@H_403_2@;
 11@H_403_2@  12@H_403_2@  13@H_403_2@  rgba(25,0.5)@H_403_2@;
 14@H_403_2@         color@H_403_2@: 15@H_403_2@     }
 16@H_403_2@ 
 17@H_403_2@     .current @H_403_2@{
 18@H_403_2@  flex@H_403_2@;
 19@H_403_2@         flex-direction@H_403_2@: column@H_403_2@;
 20@H_403_2@         vertical-align@H_403_2@: middle@H_403_2@;
 21@H_403_2@         justify-content@H_403_2@: center@H_403_2@;
 22@H_403_2@  400rpx@H_403_2@;
 23@H_403_2@         border-bottom@H_403_2@: #F1F1F1 2rpx solid@H_403_2@;
 24@H_403_2@     }
 25@H_403_2@ 
 26@H_403_2@     .current view @H_403_2@{
 27@H_403_2@         margin-bottom@H_403_2@: 28@H_403_2@     }
 29@H_403_2@ 
 30@H_403_2@     .district @H_403_2@{
 31@H_403_2@  60rpx@H_403_2@;
 32@H_403_2@  45rpx@H_403_2@;
 33@H_403_2@         text-align@H_403_2@: 34@H_403_2@ 
 35@H_403_2@     }
 36@H_403_2@ 
 37@H_403_2@     .temp @H_403_2@{
 38@H_403_2@  39@H_403_2@  70rpx@H_403_2@;
 40@H_403_2@  41@H_403_2@         line-height@H_403_2@: 1.5@H_403_2@;
 42@H_403_2@     }
 43@H_403_2@ 
 44@H_403_2@     .temp_range @H_403_2@{
 45@H_403_2@  46@H_403_2@  40rpx@H_403_2@;
 47@H_403_2@  48@H_403_2@  49@H_403_2@     }
 50@H_403_2@ 
 51@H_403_2@     .temp_desc @H_403_2@{
 52@H_403_2@  50rpx@H_403_2@;
 53@H_403_2@  30rpx@H_403_2@;
 54@H_403_2@  55@H_403_2@  56@H_403_2@     }
 57@H_403_2@ 
 58@H_403_2@     .temp_src @H_403_2@{
 59@H_403_2@  60@H_403_2@  row@H_403_2@;
 61@H_403_2@  justify@H_403_2@;
 62@H_403_2@  bottom@H_403_2@;
 63@H_403_2@     }
 64@H_403_2@ 
 65@H_403_2@     .temp_src_left @H_403_2@{}
 66@H_403_2@ 
 67@H_403_2@     .temp_src_right @H_403_2@{
 68@H_403_2@         flex@H_403_2@: 1@H_403_2@;
 69@H_403_2@  right@H_403_2@;
 70@H_403_2@     }
 71@H_403_2@ 
 72@H_403_2@     .top @H_403_2@{
 73@H_403_2@  74@H_403_2@  75@H_403_2@     }
 76@H_403_2@ 
 77@H_403_2@     .hour @H_403_2@{
 78@H_403_2@  79@H_403_2@  80@H_403_2@  81@H_403_2@  small@H_403_2@;
 82@H_403_2@         margin-top@H_403_2@: 83@H_403_2@  84@H_403_2@  85@H_403_2@     }
 86@H_403_2@     .each_hour@H_403_2@{
 87@H_403_2@         margin-left@H_403_2@: 6rpx@H_403_2@;
 88@H_403_2@     }
 89@H_403_2@     .each_hour_img@H_403_2@{
 90@H_403_2@  91@H_403_2@  92@H_403_2@     }
 93@H_403_2@     .sevenday @H_403_2@{
 94@H_403_2@  95@H_403_2@  96@H_403_2@     }
 97@H_403_2@ 
 98@H_403_2@     .each_day @H_403_2@{
 99@H_403_2@ 100@H_403_2@ 101@H_403_2@ 102@H_403_2@ 103@H_403_2@ 104@H_403_2@ 
105@H_403_2@     }
106@H_403_2@ 
107@H_403_2@ 
108@H_403_2@     .each_day_text @H_403_2@{
109@H_403_2@ 110@H_403_2@  left@H_403_2@;
111@H_403_2@  2@H_403_2@;
112@H_403_2@     }
113@H_403_2@ 
114@H_403_2@ 
115@H_403_2@     .each_day_img @H_403_2@{
116@H_403_2@ 117@H_403_2@ 118@H_403_2@     }
119@H_403_2@ 
120@H_403_2@     .each_day_temp @H_403_2@{
121@H_403_2@ 122@H_403_2@ 123@H_403_2@ 124@H_403_2@     }
125@H_403_2@ 
126@H_403_2@     .air @H_403_2@{
127@H_403_2@ 128@H_403_2@ 129@H_403_2@         margin@H_403_2@:130@H_403_2@     }
131@H_403_2@ 
132@H_403_2@     .air_title @H_403_2@{
133@H_403_2@ 134@H_403_2@ 135@H_403_2@ 136@H_403_2@     }
137@H_403_2@ 
138@H_403_2@     .air_body @H_403_2@{
139@H_403_2@ 140@H_403_2@ 141@H_403_2@     }
142@H_403_2@ 
143@H_403_2@     .air_left @H_403_2@{
144@H_403_2@ 145@H_403_2@  inline-block@H_403_2@;
146@H_403_2@ 147@H_403_2@ 148@H_403_2@     }
149@H_403_2@     .airprogress@H_403_2@{
150@H_403_2@ 151@H_403_2@ 152@H_403_2@     }
153@H_403_2@     .air_right @H_403_2@{
154@H_403_2@ 155@H_403_2@ 156@H_403_2@ 157@H_403_2@     }
158@H_403_2@ 
159@H_403_2@     .air_content @H_403_2@{
160@H_403_2@ 161@H_403_2@ 162@H_403_2@     }
163@H_403_2@ 
164@H_403_2@     .air_content_name @H_403_2@{
165@H_403_2@ 166@H_403_2@ 167@H_403_2@     }
168@H_403_2@ 
169@H_403_2@     .air_content_value @H_403_2@{
170@H_403_2@ 171@H_403_2@ 172@H_403_2@     }
173@H_403_2@ 
174@H_403_2@     
175@H_403_2@     .wind@H_403_2@{
176@H_403_2@ 177@H_403_2@ 178@H_403_2@  260rpx@H_403_2@;
179@H_403_2@ 180@H_403_2@     }
181@H_403_2@     .wind_title@H_403_2@{
182@H_403_2@ 183@H_403_2@ 184@H_403_2@     }
185@H_403_2@     .wind_body@H_403_2@{
186@H_403_2@ 187@H_403_2@ 188@H_403_2@     }
189@H_403_2@     .wind_left@H_403_2@{
190@H_403_2@ 191@H_403_2@ 192@H_403_2@  150rpx@H_403_2@;
193@H_403_2@     }
194@H_403_2@     .wind_right@H_403_2@{
195@H_403_2@ 196@H_403_2@ 197@H_403_2@ 198@H_403_2@     }
199@H_403_2@     .wind_right_direction@H_403_2@{
200@H_403_2@  0.5@H_403_2@;
201@H_403_2@ 202@H_403_2@ 203@H_403_2@     }
204@H_403_2@     .wind_right_power@H_403_2@{
205@H_403_2@ 206@H_403_2@ 207@H_403_2@ 208@H_403_2@     }
209@H_403_2@     .wind_left_img@H_403_2@{
210@H_403_2@  140rpx@H_403_2@;
211@H_403_2@ 212@H_403_2@     }
213@H_403_2@     .wind01@H_403_2@{
214@H_403_2@ 215@H_403_2@ 216@H_403_2@  0rpx@H_403_2@;
217@H_403_2@     }
218@H_403_2@     .wind02@H_403_2@{
219@H_403_2@ 220@H_403_2@  -20rpx@H_403_2@;
221@H_403_2@ 222@H_403_2@     }
223@H_403_2@     .provider@H_403_2@{
224@H_403_2@ 225@H_403_2@     }
226@H_403_2@ </style>@H_403_2@
View Code@H_403_2@

 

(四)页面调用组件@H_403_2@

当组件定义完成,就可以在页面引用组件,如下所示:

本例采用swiper来实现左右滑动功能页面(template)源码如下:

swiper @H_403_2@:indicator-dots@H_403_2@="showIndicatorDots"@H_403_2@ indicator-color@H_403_2@="#FFFFFF"@H_403_2@ indicator-active-color@H_403_2@="#FF0000"@H_403_2@ :autoplay@H_403_2@="isAutoPlay"@H_403_2@swiper-item @H_403_2@v-for@H_403_2@="(item,index) in weather_content"@H_403_2@weather @H_403_2@:id@H_403_2@="index"@H_403_2@ 
                :district@H_403_2@="item.district"@H_403_2@ 
 7@H_403_2@                 :temp@H_403_2@="item.temp"@H_403_2@ 
 8@H_403_2@                 :tempdesc@H_403_2@="item.tempdesc"@H_403_2@ 
                :temprange@H_403_2@="item.temprange"@H_403_2@
                :updatetime@H_403_2@="item.updatetime"@H_403_2@
                :timelist@H_403_2@="item.time_list"@H_403_2@
                :daylist@H_403_2@="item.day_list"@H_403_2@
                :airvalue@H_403_2@="item.air_value"@H_403_2@
                :airlist@H_403_2@="item.air_list"@H_403_2@
                :winddirection@H_403_2@="item.winddirection"@H_403_2@
                :windpower@H_403_2@="item.windpower"@H_403_2@
                class@H_403_2@="weather"@H_403_2@18@H_403_2@                     
weather@H_403_2@20@H_403_2@             swiper-item@H_403_2@21@H_403_2@         swiper@H_403_2@22@H_403_2@     >@H_403_2@
View Code@H_403_2@

本例通过脚本造了一些数据,没有进行接口调用,脚本(JavaScript)代码如下:

@H_404_2077@

  1@H_403_2@ <script>
  2@H_403_2@     import weather from "@/components/weather/weather.vue"
  3@H_403_2@     export   4@H_403_2@   5@H_403_2@             weather
@H_403_2@  6@H_403_2@   7@H_403_2@   8@H_403_2@               9@H_403_2@                 title: 'Hello' 10@H_403_2@                 showIndicatorDots: 11@H_403_2@                 isAutoPlay:false@H_403_2@ 12@H_403_2@                 weather_content:[{
@H_403_2@ 13@H_403_2@                     district:"龙岗区" 14@H_403_2@                     temp:23 15@H_403_2@                     temprange:"-2°C / 10°C" 16@H_403_2@                     tempdesc:"晴 空气良" 17@H_403_2@                     updatetime:"22:10" 18@H_403_2@                     time_list: [{
@H_403_2@ 19@H_403_2@                             time: "00:00" 20@H_403_2@                             img: "../../static/day/00.png" 21@H_403_2@                             temp: "0°C"
 22@H_403_2@                         },1)"> 23@H_403_2@                         {
@H_403_2@ 24@H_403_2@                             time: "01:00" 25@H_403_2@                             img: "../../static/day/01.png" 26@H_403_2@                             temp: "1°C"
 27@H_403_2@ 403_2@ 28@H_403_2@                             time: "02:00" 29@H_403_2@                             img: "../../static/day/02.png" 30@H_403_2@                             temp: "2°C"
 31@H_403_2@  32@H_403_2@  33@H_403_2@                             time: "03:00" 34@H_403_2@                             img: "../../static/day/03.png" 35@H_403_2@                             temp: "3°C"
 36@H_403_2@  37@H_403_2@                             time: "04:00" 38@H_403_2@                             img: "../../static/day/04.png" 39@H_403_2@                             temp: "4°C"
 40@H_403_2@  41@H_403_2@  42@H_403_2@                             time: "05:00" 43@H_403_2@                             img: "../../static/day/05.png" 44@H_403_2@                             temp: "5°C"
 45@H_403_2@  46@H_403_2@                             time: "06:00" 47@H_403_2@                             img: "../../static/day/06.png" 48@H_403_2@                             temp: "6°C"
 49@H_403_2@  50@H_403_2@  51@H_403_2@                             time: "07:00" 52@H_403_2@                             img: "../../static/day/07.png" 53@H_403_2@                             temp: "7°C"
 54@H_403_2@  55@H_403_2@                             time: "08:00" 56@H_403_2@                             img: "../../static/day/08.png" 57@H_403_2@                             temp: "8°C"
 58@H_403_2@  59@H_403_2@  60@H_403_2@                             time: "09:00" 61@H_403_2@                             img: "../../static/day/09.png" 62@H_403_2@                             temp: "9°C"
 63@H_403_2@  64@H_403_2@                             time: "10:00" 65@H_403_2@                             img: "../../static/day/10.png" 66@H_403_2@                             temp: "10°C"
 67@H_403_2@  68@H_403_2@  69@H_403_2@                             time: "11:00" 70@H_403_2@                             img: "../../static/day/11.png" 71@H_403_2@                             temp: "11°C"
 72@H_403_2@  73@H_403_2@                             time: "12:00" 74@H_403_2@                             img: "../../static/day/12.png" 75@H_403_2@                             temp: "12°C"
 76@H_403_2@  77@H_403_2@  78@H_403_2@                             time: "13:00" 79@H_403_2@                             img: "../../static/day/13.png" 80@H_403_2@                             temp: "13°C"
 81@H_403_2@  82@H_403_2@                             time: "14:00" 83@H_403_2@                             img: "../../static/day/14.png" 84@H_403_2@                             temp: "14°C"
 85@H_403_2@  86@H_403_2@  87@H_403_2@                             time: "15:00" 88@H_403_2@                             img: "../../static/day/15.png" 89@H_403_2@                             temp: "15°C"
 90@H_403_2@  91@H_403_2@                             time: "16:00" 92@H_403_2@                             img: "../../static/day/16.png" 93@H_403_2@                             temp: "16°C"
 94@H_403_2@  95@H_403_2@  96@H_403_2@                             time: "17:00" 97@H_403_2@                             img: "../../static/day/17.png" 98@H_403_2@                             temp: "17°C"
 99@H_403_2@ 100@H_403_2@                             time: "18:00"101@H_403_2@                             img: "../../static/day/18.png"102@H_403_2@                             temp: "18°C"
103@H_403_2@ 104@H_403_2@ 105@H_403_2@                             time: "19:00"106@H_403_2@                             img: "../../static/day/19.png"107@H_403_2@                             temp: "19°C"
108@H_403_2@ 109@H_403_2@                             time: "20:00"110@H_403_2@                             img: "../../static/day/20.png"111@H_403_2@                             temp: "20°C"
112@H_403_2@ 113@H_403_2@ 114@H_403_2@                             time: "21:00"115@H_403_2@                             img: "../../static/day/21.png"116@H_403_2@                             temp: "21°C"
117@H_403_2@ 118@H_403_2@                             time: "22:00"119@H_403_2@                             img: "../../static/day/22.png"120@H_403_2@                             temp: "22°C"
121@H_403_2@ 122@H_403_2@ 123@H_403_2@                             time: "23:00"124@H_403_2@                             img: "../../static/day/23.png"125@H_403_2@                             temp: "23°C"
126@H_403_2@                         }
@H_403_2@127@H_403_2@                     ],1)">128@H_403_2@                     day_list: [{
@H_403_2@129@H_403_2@                             day: "10月31日"130@H_403_2@                             week: "昨天"131@H_403_2@                             img: "../../static/night/00.png"132@H_403_2@                             temp: "26°C/21°C"
133@H_403_2@ 134@H_403_2@ 135@H_403_2@                             day: "11月01日"136@H_403_2@                             week: "今天"137@H_403_2@                             img: "../../static/night/01.png"138@H_403_2@                             temp: "22°C/11°C"
139@H_403_2@ 140@H_403_2@ 141@H_403_2@                             day: "11月02日"142@H_403_2@                             week: "明天"143@H_403_2@                             img: "../../static/night/03.png"144@H_403_2@                             temp: "12°C/11°C"
145@H_403_2@ 146@H_403_2@ 147@H_403_2@                             day: "11月03日"148@H_403_2@                             week: "星期二"149@H_403_2@                             img: "../../static/night/04.png"150@H_403_2@                             temp: "18°C/01°C"
151@H_403_2@ 152@H_403_2@ 153@H_403_2@                             day: "11月04日"154@H_403_2@                             week: "星期三"155@H_403_2@                             img: "../../static/night/06.png"156@H_403_2@                             temp: "22°C/02°C"
157@H_403_2@ 158@H_403_2@ 159@H_403_2@                             day: "11月05日"160@H_403_2@                             week: "星期四"161@H_403_2@                             img: "../../static/night/07.png"162@H_403_2@                             temp: "12°C/02°C"
163@H_403_2@ 164@H_403_2@ 165@H_403_2@                             day: "11月07日"166@H_403_2@                             week: "星期五"167@H_403_2@                             img: "../../static/night/09.png"168@H_403_2@                             temp: "06°C/02°C"
169@H_403_2@ 170@H_403_2@ 171@H_403_2@                     air_value:30172@H_403_2@                     air_list: [{
@H_403_2@173@H_403_2@                             name: "PM10"174@H_403_2@                             value: 23
175@H_403_2@ 176@H_403_2@ 177@H_403_2@                             name: "PM2.5"178@H_403_2@                             value: 25
179@H_403_2@ 180@H_403_2@ 181@H_403_2@                             name: "NO2"182@H_403_2@                             value: 28
183@H_403_2@ 184@H_403_2@ 185@H_403_2@                             name: "SO2"186@H_403_2@                             value: 5
187@H_403_2@ 188@H_403_2@ 189@H_403_2@                             name: "O3"190@H_403_2@                             value: 35
191@H_403_2@ 192@H_403_2@ 193@H_403_2@                             name: "CO"194@H_403_2@                             value: 0.91
195@H_403_2@ 196@H_403_2@ 197@H_403_2@                     winddirection:"北风"198@H_403_2@                     windpower:"3~4级"
199@H_403_2@                 },1)">200@H_403_2@                     district:"东城区"201@H_403_2@                     temp:13202@H_403_2@                     temprange:"12°C / 20°C"203@H_403_2@                     tempdesc:"阴 空气很好"204@H_403_2@                     updatetime:"22:00"205@H_403_2@ 206@H_403_2@                             time: "00:00"207@H_403_2@                             img: "../../static/night/00.png"208@H_403_2@                             temp: "0°C"
209@H_403_2@ 210@H_403_2@ 211@H_403_2@                             time: "01:00"212@H_403_2@                             img: "../../static/night/01.png"213@H_403_2@                             temp: "1°C"
214@H_403_2@ 215@H_403_2@                             time: "02:00"216@H_403_2@                             img: "../../static/night/02.png"217@H_403_2@                             temp: "2°C"
218@H_403_2@ 219@H_403_2@ 220@H_403_2@                             time: "03:00"221@H_403_2@                             img: "../../static/night/03.png"222@H_403_2@                             temp: "3°C"
223@H_403_2@ 224@H_403_2@                             time: "04:00"225@H_403_2@                             img: "../../static/night/04.png"226@H_403_2@                             temp: "4°C"
227@H_403_2@ 228@H_403_2@ 229@H_403_2@                             time: "05:00"230@H_403_2@                             img: "../../static/night/05.png"231@H_403_2@                             temp: "5°C"
232@H_403_2@ 233@H_403_2@                             time: "06:00"234@H_403_2@                             img: "../../static/night/06.png"235@H_403_2@                             temp: "6°C"
236@H_403_2@ 237@H_403_2@ 238@H_403_2@                             time: "07:00"239@H_403_2@                             img: "../../static/night/07.png"240@H_403_2@                             temp: "7°C"
241@H_403_2@ 242@H_403_2@                             time: "08:00"243@H_403_2@                             img: "../../static/night/08.png"244@H_403_2@                             temp: "8°C"
245@H_403_2@ 246@H_403_2@ 247@H_403_2@                             time: "09:00"248@H_403_2@                             img: "../../static/night/09.png"249@H_403_2@                             temp: "9°C"
250@H_403_2@ 251@H_403_2@                             time: "10:00"252@H_403_2@                             img: "../../static/night/10.png"253@H_403_2@                             temp: "10°C"
254@H_403_2@ 255@H_403_2@ 256@H_403_2@                             time: "11:00"257@H_403_2@                             img: "../../static/night/11.png"258@H_403_2@                             temp: "11°C"
259@H_403_2@ 260@H_403_2@                             time: "12:00"261@H_403_2@                             img: "../../static/night/12.png"262@H_403_2@                             temp: "12°C"
263@H_403_2@ 264@H_403_2@ 265@H_403_2@                             time: "13:00"266@H_403_2@                             img: "../../static/night/13.png"267@H_403_2@                             temp: "13°C"
268@H_403_2@ 269@H_403_2@                             time: "14:00"270@H_403_2@                             img: "../../static/night/14.png"271@H_403_2@                             temp: "14°C"
272@H_403_2@ 273@H_403_2@ 274@H_403_2@                             time: "15:00"275@H_403_2@                             img: "../../static/night/15.png"276@H_403_2@                             temp: "15°C"
277@H_403_2@ 278@H_403_2@                             time: "16:00"279@H_403_2@                             img: "../../static/night/16.png"280@H_403_2@                             temp: "16°C"
281@H_403_2@ 282@H_403_2@ 283@H_403_2@                             time: "17:00"284@H_403_2@                             img: "../../static/night/17.png"285@H_403_2@                             temp: "17°C"
286@H_403_2@ 287@H_403_2@                             time: "18:00"288@H_403_2@                             img: "../../static/night/18.png"289@H_403_2@                             temp: "18°C"
290@H_403_2@ 291@H_403_2@ 292@H_403_2@                             time: "19:00"293@H_403_2@                             img: "../../static/night/19.png"294@H_403_2@                             temp: "19°C"
295@H_403_2@ 296@H_403_2@                             time: "20:00"297@H_403_2@                             img: "../../static/night/20.png"298@H_403_2@                             temp: "20°C"
299@H_403_2@ 300@H_403_2@ 301@H_403_2@                             time: "21:00"302@H_403_2@                             img: "../../static/night/21.png"303@H_403_2@                             temp: "21°C"
304@H_403_2@ 305@H_403_2@                             time: "22:00"306@H_403_2@                             img: "../../static/night/22.png"307@H_403_2@                             temp: "22°C"
308@H_403_2@ 309@H_403_2@ 310@H_403_2@                             time: "23:00"311@H_403_2@                             img: "../../static/night/23.png"312@H_403_2@                             temp: "23°C"
313@H_403_2@ 314@H_403_2@ 315@H_403_2@ 316@H_403_2@                             day: "10月31日"317@H_403_2@                             week: "昨天"318@H_403_2@                             img: "../../static/day/00.png"319@H_403_2@                             temp: "6°C/21°C"
320@H_403_2@ 321@H_403_2@ 322@H_403_2@                             day: "11月01日"323@H_403_2@                             week: "今天"324@H_403_2@                             img: "../../static/day/01.png"325@H_403_2@                             temp: "12°C/11°C"
326@H_403_2@ 327@H_403_2@ 328@H_403_2@                             day: "11月02日"329@H_403_2@                             week: "明天"330@H_403_2@                             img: "../../static/day/03.png"331@H_403_2@                             temp: "22°C/09°C"
332@H_403_2@ 333@H_403_2@ 334@H_403_2@                             day: "11月03日"335@H_403_2@                             week: "星期二"336@H_403_2@                             img: "../../static/day/04.png"337@H_403_2@                             temp: "28°C/11°C"
338@H_403_2@ 339@H_403_2@ 340@H_403_2@                             day: "11月04日"341@H_403_2@                             week: "星期三"342@H_403_2@                             img: "../../static/day/06.png"343@H_403_2@                             temp: "12°C/02°C"
344@H_403_2@ 345@H_403_2@ 346@H_403_2@                             day: "11月05日"347@H_403_2@                             week: "星期四"348@H_403_2@                             img: "../../static/day/07.png"349@H_403_2@                             temp: "22°C/12°C"
350@H_403_2@ 351@H_403_2@ 352@H_403_2@                             day: "11月07日"353@H_403_2@                             week: "星期五"354@H_403_2@                             img: "../../static/night/09.png"355@H_403_2@                             temp: "16°C/12°C"
356@H_403_2@ 357@H_403_2@ 358@H_403_2@                     air_value:67359@H_403_2@ 360@H_403_2@                             name: "PM10"361@H_403_2@                             value: 63
362@H_403_2@ 363@H_403_2@ 364@H_403_2@                             name: "PM2.5"365@H_403_2@                             value: 39
366@H_403_2@ 367@H_403_2@ 368@H_403_2@                             name: "NO2"369@H_403_2@                             value: 23
370@H_403_2@ 371@H_403_2@ 372@H_403_2@                             name: "SO2"373@H_403_2@                             value: 5
374@H_403_2@ 375@H_403_2@ 376@H_403_2@                             name: "O3"377@H_403_2@                             value: 65
378@H_403_2@ 379@H_403_2@ 380@H_403_2@                             name: "CO"381@H_403_2@                             value: 0.71
382@H_403_2@ 383@H_403_2@ 384@H_403_2@                     winddirection:"东南风"385@H_403_2@                     windpower:"1~4级"
386@H_403_2@                 }]
@H_403_2@387@H_403_2@ 388@H_403_2@ 389@H_403_2@         onLoad() {
@H_403_2@390@H_403_2@             console.log("测试加载页面")
@H_403_2@391@H_403_2@ 392@H_403_2@         onShow(){
@H_403_2@393@H_403_2@             console.log("页面onshow...."394@H_403_2@ 395@H_403_2@         methods: {
@H_403_2@396@H_403_2@ 
397@H_403_2@ 398@H_403_2@ 399@H_403_2@ </script>
View Code@H_403_2@

样式(CSS)代码如下:

    .content @H_403_2@{
 #007AFF@H_403_2@;
 6@H_403_2@     }
 7@H_403_2@     swiper@H_403_2@{
10@H_403_2@     }
    .swiper-item@H_403_2@{
        border@H_403_2@: #007AFF 1rpx solid@H_403_2@;
13@H_403_2@     }
14@H_403_2@     .weather@H_403_2@{
16@H_403_2@     }
17@H_403_2@ (五)注意事项@H_403_2@

例子虽小,开发时也会踩坑,具体事项如下:

1. 开发过程中,在运行到Chrome浏览器,一切正常,但是当运行到Android真机时,页面除了导航条显示,其他一片空白,后来发现,需要在App.vue中定义页面的高度,才可以正常显示。App.vue源码如下所示:

<script>
@H_403_2@    export default @H_403_2@{
        onLaunch@H_403_2@: function() {
@H_403_2@            console.log('App Launch')
@H_403_2@ 5@H_403_2@         } 6@H_403_2@         onShow: function() @H_403_2@{
            console.log('App Show')
@H_403_2@ 8@H_403_2@         } 9@H_403_2@         onHide: function() @H_403_2@{
            console.log('App Hide')
@H_403_2@11@H_403_2@         }
13@H_403_2@ </script>
@H_403_2@14@H_403_2@ 
16@H_403_2@     每个页面公共css @H_403_2@    uni-page-body,#app @H_403_2@{width@H_403_2@:100%@H_403_2@;height@H_403_2@: 100%@H_403_2@;}
18@H_403_2@      #ifdef APP-PLUS @H_403_2@19@H_403_2@     
20@H_403_2@      以下样式用于 hello uni-app 演示所需 @H_403_2@21@H_403_2@     page @H_403_2@{
 background-color: #F4F5F6; @H_403_2@ font-size: 28rpx; @H_403_2@25@H_403_2@          line-height: 1.8; @H_403_2@26@H_403_2@     }
27@H_403_2@      #endif@H_403_2@28@H_403_2@ </style>@H_403_2@
View Code@H_403_2@

2. 在开发过程中,最初圆形进度条是采用svg进行开发,在Chrome浏览器显示正常,但是在手机App上显示不出来,并造成页面显示一大片空白,后来不得已采用css实现。

备注@H_403_2@

次北固山下
【作者】王湾  【朝代】唐 
客路青山外,行舟绿水前。
潮平两岸阔,风正一帆悬。
海日生残夜,江春入旧年。
乡书何处达?归雁洛阳边。

猜你在找的微信小程序相关文章