微信小程序生成一個天氣查詢的小程序
基本的頁面結(jié)構(gòu)和邏輯
頁面結(jié)構(gòu):包括一個輸入框和一個查詢按鈕。
頁面邏輯:在用戶輸入城市名稱后,點(diǎn)擊查詢按鈕,跳轉(zhuǎn)到天氣詳情頁面,并將城市名稱作為參數(shù)傳遞。
主要代碼
index.js
// index.js
Page({
data: {
city: ''
},
onInput: function(e) {
this.setData({
city: e.detail.value
});
},
onSearch: function() {
wx.navigateTo({
url: '/pages/weather?city=' + this.data.city
});
}
});
index.wxml
<!-- index.wxml -->
<view class="container">
<input type="text" placeholder="請輸入城市名稱" bindinput="onInput"></input>
<button bindtap="onSearch">查詢</button>
</view>
index.wxss
/* index.wxss */
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
天氣詳情頁面(pages/weather)
weather.js
// weather.js
Page({
data: {
city: '',
weather: ''
},
onLoad: function(options) {
const city = options.city;
this.setData({
city: city
});
// 請求天氣數(shù)據(jù)
wx.request({
url: 'https://api.weather.com/v1/current?city=' + city,
success: res => {
this.setData({
weather: res.data.weather
});
}
});
}
});
weather.wxml
<!-- weather.wxml -->
<view class="container">
<view class="weather-info">{{ weather }}</view>
</view>
weather.wxss
/* weather.wxss */
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.weather-info {
font-size: 20px;
}
解釋
首頁和天氣詳情頁。用戶可以在首頁輸入城市名稱后,點(diǎn)擊查詢按鈕跳轉(zhuǎn)到天氣詳情頁面,并展示該城市的實(shí)時天氣信息。
請注意,實(shí)際使用中,您需要將請求天氣數(shù)據(jù)的 API 地址和參數(shù)進(jìn)行替換為真實(shí)可用的天氣數(shù)據(jù)接口。文章來源:http://www.zghlxwxcb.cn/news/detail-760972.html
到這里也就結(jié)束了,希望對您有所幫助。文章來源地址http://www.zghlxwxcb.cn/news/detail-760972.html
到了這里,關(guān)于微信小程序生成一個天氣查詢的小程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!