鸿蒙初开,开天辟地
import getWeatherUtil from "../viewmodel/getWeatherUtil"; import { weatherModel } from '../viewmodel/weatherModel'; @Entry @Component struct Index { // 城市代码集合 @State cityCodeList:number[] = [110000,120000]; // 城市名字集合 @State cityNameList:string[] = ["北京","天津"]; // 城市信息集合 @State cityWeatherList:Array = []; // 当前城市索引 @State cityIndex:number = 0; tabController:TabsController = new TabsController(); // 按钮样式 @Builder tabBuild(index:number){ Circle({width:10,height:10}).fill(this.cityIndex === index?Color.White:Color.Gray).opacity(0.4); } // 获取数据 aboutToAppear(): void { this.initDate(); } // 初始化方法 async initDate(){ let result:Array = await getWeatherUtil.getWeathers(this.cityCodeList); for (let i = 0; i < result.length i let weather:weathermodel='result[i];' this.cityweatherlist.pushweather this.citynamelist.pushweather.forecasts0.city build column row button.fontsize25.fontcolorcolor.gray.opacity0.7.backgroundcolor87ceeb.marginbottom:15 textthis.citynamelistthis.cityindex.fontsize40.fontcolorcolor.orange button.fontsize25.fontcolorcolor.gray.opacity0.7.backgroundcolor87ceeb.marginbottom:15 .width100.height10.justifycontentflexalign.spacebetween tabsbarposition:barposition.startcontroller:this.tabcontroller>{ TabContent(){ Text(cityWeather.forecasts[0].city).fontSize(40); }.tabBar(this.tabBuild(this.cityWeatherList.findIndex(obj=>obj===cityWeather))); }); }.barWidth(20).barHeight(40).onChange((index:number)=>{ this.cityIndex = index; }); }.width("100%").height("100%").backgroundColor("#87CEEB"); } }
import getWeatherUtil from "../viewmodel/getWeatherUtil";
import { weatherModel } from '../viewmodel/weatherModel';@Entry
@Component
struct Index {
// 城市代码集合
@State cityCodeList:number[] = [110000,120000];
// 城市名字集合
@State cityNameList:string[] = ["北京","天津"];
// 城市信息集合
@State cityWeatherList:Array = [];
// 当前城市索引
@State cityIndex:number = 0;
tabController:TabsController = new TabsController();
// 按钮样式
@Builder tabBuild(index:number){
Circle({width:10,height:10}).fill(this.cityIndex === index?Color.White:Color.Gray).opacity(0.4);
}
// 获取数据
aboutToAppear(): void {
this.initDate();
}
// 初始化方法
async initDate(){
let result:Array = await getWeatherUtil.getWeathers(this.cityCodeList);
for (let i = 0; i < result.length i let weather:weathermodel='result[i];' this.cityweatherlist.pushweather this.citynamelist.pushweather.forecasts0.city build column row button.fontsize25.fontcolorcolor.gray.opacity0.7.backgroundcolor87ceeb.marginbottom:15 textthis.citynamelistthis.cityindex.fontsize40.fontcolorcolor.orange button.fontsize25.fontcolorcolor.gray.opacity0.7.backgroundcolor87ceeb.marginbottom:15 .width100.height10.justifycontentflexalign.spacebetween tabsbarposition:barposition.startcontroller:this.tabcontroller>{
TabContent(){
Text(cityWeather.forecasts[0].city).fontSize(40);
}.tabBar(this.tabBuild(this.cityWeatherList.findIndex(obj=>obj===cityWeather)));
});
}.barWidth(20).barHeight(40).onChange((index:number)=>{
this.cityIndex = index;
});
}.width("100%").height("100%").backgroundColor("#87CEEB");
}
}