🔪
这个属于网络应用
首先,Vue比我想象中的简单好用多了
先使用axios获取一个列表,v-for指令会自动检测key,只需要写一遍即可
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<center> <div id="app"> <h1>天知道</h1> <input type="text" placeholder="待输入" v-model="city" @keyup.enter="searchWeather(city,reg)" /><button @click="searchWeather(city,reg)">查询</button> <ul> <li v-for="(v,k) in weatherList">{{ v.date }} <p>{{v.type}}</p> <p>{{v.low}}</p> <p>{{v.high}}</p> <p>{{v.fengli}}</p> <p>{{v.fengxiang}}</p> </li> </ul>
</div> </center> <script src="./Main.js"></script>
|
在js中,并不需要过多复杂的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| let app = new Vue({ el: "#app", data: { city: "", reg: /^[\u4E00-\u9FA5]+$/,
weatherList: [],
}, methods: { searchWeather(city, reg) { var that = this if (city.length != 0 && reg.test(city)) { axios.get('http://wthrcdn.etouch.cn/weather_mini?city=' + city) .then(function(response) { that.weatherList = response.data.data.forecast console.log(that.weatherList[0].date); console.log(that.weatherList); }) .catch(function(err) {}) } else { alert("请检查输入为空或者输入的是非汉字"); }
} }
})
|