元姐加油吖 發表於 2021-2-9 08:01:00

uni-app 自定义公用模块

<p>在uni-app 项目跟目录下创建common目录,然后再common目录下创建common.js用于定义公用的方法。</p>
<p><img src="https://img2020.cnblogs.com/blog/2259678/202102/2259678-20210209075716697-186759748.png"></p>
<p>&nbsp;</p>
<p><strong>&nbsp;common.js可以定义常量和方法:</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接口地址</span>
const apiUrl=''<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义公用方法</span>
const sayHi=<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
    console.log(</span>'hi'<span style="color: rgba(0, 0, 0, 1)">);
}


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输出</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">{
    apiUrl,
    sayHi
}</span></pre>
</div>
<p><strong>在使用的vue中调用该模块</strong></p>
<div class="cnblogs_code">
<pre>import common from "../../common/common.js";</pre>
</div>
<p><strong>使用公用模块的常量或方法:</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">data() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">{
                loadingTxt: </span>'加载更多'<span style="color: rgba(0, 0, 0, 1)">,
                newsList: [],
                common.apiUrl
            }
      },</span></pre>
</div>
<p>全部代码:</p>
<div class="cnblogs_code">
<pre>&lt;template&gt;
    &lt;view style="flex-wrap: wrap;"&gt;
      &lt;!-- 轮播图 --&gt;
      &lt;swiper indicator-dots="true" autoplay="true" &gt;
            &lt;swiper-item&gt;
                &lt;image src="/static/image/BasicsBg.png"&gt;&lt;/image&gt;
            &lt;/swiper-item&gt;
            &lt;swiper-item&gt;
                &lt;image src="/static/image/componentBg.png"&gt;&lt;/image&gt;
            &lt;/swiper-item&gt;
            &lt;swiper-item&gt;<span style="color: rgba(0, 0, 0, 1)">
                冷链App
            </span>&lt;/swiper-item&gt;
      &lt;/swiper&gt;
      &lt;!-- 显示数据 --&gt;
      &lt;!-- &lt;navigator class="news-list" :url="'./info?newsid='+item.newsid" v-<span style="color: rgba(0, 0, 255, 1)">for</span>="(item,index) in newsList"&gt; --&gt;
      &lt;navigator class="news-list" url="./info" v-<span style="color: rgba(0, 0, 255, 1)">for</span>="(item,index) in newsList"&gt;
            &lt;image src="../../static/image/component_cur.png" mode="widthFix"&gt;&lt;/image&gt;
            &lt;View class="news-title"&gt;<span style="color: rgba(0, 0, 0, 1)">
                {{item}}
            </span>&lt;/View&gt;
      &lt;/navigator&gt;
      
      
      &lt;!-- 加载视图 --&gt;
      &lt;view class="loading"&gt;{{loadingTxt}}&lt;/view&gt;
    &lt;/view&gt;
&lt;/template&gt;

&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
    import common from </span>"../../common/common.js"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> _self,page=1,timer=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
      data() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">{
                loadingTxt: </span>'加载更多'<span style="color: rgba(0, 0, 0, 1)">,
                newsList: [],
                common.apiUrl
            }
      },
      onLoad:</span><span style="color: rgba(0, 0, 255, 1)">function</span>(){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化加载</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getNews();
            _self</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
            uni.showLoading({
                title:</span>'欢迎使用App'<span style="color: rgba(0, 0, 0, 1)">
            });
            setTimeout(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
                uni.hideLoading();
            }, </span>2000<span style="color: rgba(0, 0, 0, 1)">);
      },
      onPullDownRefresh:</span><span style="color: rgba(0, 0, 255, 1)">function</span>(){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">下拉刷新监听方法</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getNews();
      },
      onReachBottom: </span><span style="color: rgba(0, 0, 255, 1)">function</span>(){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">上拉加载监听方法</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getMoreNews();
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(timer!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){clearTimeout(timer);}
            timer</span>=setTimeout(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
                _self.getMoreNews();
            },</span>5000<span style="color: rgba(0, 0, 0, 1)">);
      },
      methods:{
            getNews:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
                page</span>=1<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示加载中动画</span>
<span style="color: rgba(0, 0, 0, 1)">                uni.showNavigationBarLoading();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">请求数据</span>
<span style="color: rgba(0, 0, 0, 1)">                uni.request({
                  url: </span>'https://demo.hcoder.net/index.php?user=hcoder&amp;pwd=hcoder&amp;m=list1&amp;page='+<span style="color: rgba(0, 0, 0, 1)">page,
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">url: 'https://www.easy-mock.com/mock/5bb833775df5622d84ac87ca/example/news#!method=get',</span>
                  success:<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res){
                        console.log(res);
                        </span><span style="color: rgba(0, 0, 255, 1)">var</span> newsList=res.data.split('--hcSplitor--'<span style="color: rgba(0, 0, 0, 1)">);;
                        _self.newsList</span>=<span style="color: rgba(0, 0, 0, 1)">newsList;
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">成功获取数据后结束下拉刷新</span>
<span style="color: rgba(0, 0, 0, 1)">                        uni.stopPullDownRefresh();
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">成功获取数据后隐藏加载动画</span>
<span style="color: rgba(0, 0, 0, 1)">                        uni.hideNavigationBarLoading();
                        page</span>++<span style="color: rgba(0, 0, 0, 1)">;
                  }
                })
            },
            getMoreNews:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断是否已经加载全部</span>
                <span style="color: rgba(0, 0, 255, 1)">if</span>(_self.loadingTxt=='已经加载全部'){<span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;}
                _self.loadingTxt</span>='加载中'<span style="color: rgba(0, 0, 0, 1)">;
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示加载中动画</span>
<span style="color: rgba(0, 0, 0, 1)">                uni.showNavigationBarLoading();
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">请求数据</span>
<span style="color: rgba(0, 0, 0, 1)">                uni.request({
                  url: </span>'https://demo.hcoder.net/index.php?user=hcoder&amp;pwd=hcoder&amp;m=list1&amp;page='+<span style="color: rgba(0, 0, 0, 1)">page,
                  success:</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res){
                        console.log(res);
                        uni.hideNavigationBarLoading();
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span>(res.data==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                            _self.loadingTxt</span>='已经加载全部'<span style="color: rgba(0, 0, 0, 1)">;
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
                        }
                        </span><span style="color: rgba(0, 0, 255, 1)">var</span> newsList=res.data.split('--hcSplitor--'<span style="color: rgba(0, 0, 0, 1)">);
                        _self.newsList</span>=<span style="color: rgba(0, 0, 0, 1)">_self.newsList.concat(newsList);
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">成功获取数据后结束下拉刷新</span>
<span style="color: rgba(0, 0, 0, 1)">                        uni.stopPullDownRefresh();
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">成功获取数据后隐藏加载动画</span>
<span style="color: rgba(0, 0, 0, 1)">                        uni.hideNavigationBarLoading();
                        page</span>++<span style="color: rgba(0, 0, 0, 1)">;
                        _self.loadingTxt</span>='加载更多'<span style="color: rgba(0, 0, 0, 1)">;
                  }
                })
            }
            
            
      }
    }
</span>&lt;/script&gt;

&lt;style&gt;<span style="color: rgba(0, 0, 0, 1)">
view{width:</span>100%<span style="color: rgba(0, 0, 0, 1)">}
swiper</span>-<span style="color: rgba(0, 0, 0, 1)">item{
    background: #00FF00;
    height: 200px;
    width: </span>100%<span style="color: rgba(0, 0, 0, 1)">;
}
swiper</span>-<span style="color: rgba(0, 0, 0, 1)">item image{
    width: </span>100%<span style="color: rgba(0, 0, 0, 1)">;
}
.loading{
    line</span>-<span style="color: rgba(0, 0, 0, 1)">height: 2em;
    text</span>-<span style="color: rgba(0, 0, 0, 1)">align: center;
    color:#</span>888<span style="color: rgba(0, 0, 0, 1)">;
    margin</span>-<span style="color: rgba(0, 0, 0, 1)">top: 30px;
}
.news</span>-<span style="color: rgba(0, 0, 0, 1)">list{
    display: flex;
    width: </span>94%<span style="color: rgba(0, 0, 0, 1)">;
    padding: 1upx </span>3%<span style="color: rgba(0, 0, 0, 1)">;
    flex</span>-<span style="color: rgba(0, 0, 0, 1)">wrap: nowrap;
    margin: 12upx </span>0<span style="color: rgba(0, 0, 0, 1)">;
}
.news</span>-<span style="color: rgba(0, 0, 0, 1)">list image{
    width: 150upx;
    margin</span>-<span style="color: rgba(0, 0, 0, 1)">right: 12upx;
    flex</span>-shrink: 0<span style="color: rgba(0, 0, 0, 1)">;
}
.news</span>-<span style="color: rgba(0, 0, 0, 1)">title{
    width: </span>100%<span style="color: rgba(0, 0, 0, 1)">;
    height: auto;
    margin</span>-<span style="color: rgba(0, 0, 0, 1)">top: 10px;
    font</span>-<span style="color: rgba(0, 0, 0, 1)">size: 28upx;
}
</span>&lt;/style&gt;</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/ckfuture/p/14391755.html
頁: [1]
查看完整版本: uni-app 自定义公用模块