4.gif

    1. <template>
    2. <div>
    3. <p class="name">热门城市</p>
    4. <router-link
    5. to="/home/nowplaying"
    6. v-for="vas of hotcities"
    7. :key="vas.id"
    8. @click.native="handleClick(vas.name)"
    9. >
    10. <span class="city">{{vas.name}}</span>
    11. </router-link>
    12. <van-index-bar v-for="(item,index) of cities" :key="index">
    13. <van-index-anchor :index="index" />
    14. <router-link
    15. to="/home/nowplaying"
    16. @click.native="handleClick(value.name)"
    17. v-for="value of item"
    18. :key="value.id"
    19. >
    20. <van-cell :title="value.name" />
    21. </router-link>
    22. </van-index-bar>
    23. </div>
    24. </template>
    25. <script>
    26. export default {
    27. name: "location",
    28. data() {
    29. return {
    30. hotcities: [],
    31. cities: []
    32. };
    33. },
    34. mounted() {
    35. this.axios.get("/api/city").then(res => {
    36. this.hotcities = res.data.data.hotCities;
    37. console.log(this.hotcities);
    38. this.cities = res.data.data.cities;
    39. console.log(this.cities);
    40. });
    41. },
    42. methods: {
    43. handleClick(city) {
    44. this.$store.dispatch("changeCity", city);
    45. console.log(city);
    46. }
    47. }
    48. };
    49. </script>
    50. <style scoped>
    51. .city{
    52. font-size: 15px;
    53. color: black;
    54. padding-left: 10px;
    55. }
    56. </style>
    1. <router-link class="btn" to="/location">{{this.$store.state.city}}</router-link>
    2. <van-swipe class="pic-wrap">
    3. .btn{
    4. position: absolute;
    5. left: 10px;
    6. top: 10px;
    7. z-index: 500;
    8. padding-left: 20px;
    9. padding-right: 20px;
    10. line-height: 30px;
    11. border-radius: 30px;
    12. font-size: 15px;
    13. color: #fff;
    14. background-color:rgba(15, 15, 15, 0.3);
    15. }
    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. Vue.use(Vuex)
    4. function getCity(){
    5. let defaultCity = "武汉";
    6. if(localStorage.getItem("city")){
    7. defaultCity = localStorage.getItem("city")
    8. }
    9. return defaultCity
    10. }
    11. export default new Vuex.Store({
    12. state: {
    13. isLoading:true,
    14. city:getCity()
    15. },
    16. mutations: {
    17. toggleCity(state,city){
    18. state.city = city;
    19. }
    20. },
    21. actions: {
    22. changeCity(ctx,city){
    23. ctx.commit("toggleCity",city)
    24. localStorage.setItem("city",city)
    25. }
    26. },
    27. modules: {
    28. }
    29. })