
<template> <div> <p class="name">热门城市</p> <router-link to="/home/nowplaying" v-for="vas of hotcities" :key="vas.id" @click.native="handleClick(vas.name)" > <span class="city">{{vas.name}}</span> </router-link> <van-index-bar v-for="(item,index) of cities" :key="index"> <van-index-anchor :index="index" /> <router-link to="/home/nowplaying" @click.native="handleClick(value.name)" v-for="value of item" :key="value.id" > <van-cell :title="value.name" /> </router-link> </van-index-bar> </div></template><script>export default { name: "location", data() { return { hotcities: [], cities: [] }; }, mounted() { this.axios.get("/api/city").then(res => { this.hotcities = res.data.data.hotCities; console.log(this.hotcities); this.cities = res.data.data.cities; console.log(this.cities); }); }, methods: { handleClick(city) { this.$store.dispatch("changeCity", city); console.log(city); } }};</script><style scoped>.city{ font-size: 15px; color: black; padding-left: 10px;}</style>
<router-link class="btn" to="/location">{{this.$store.state.city}}</router-link><van-swipe class="pic-wrap">.btn{ position: absolute; left: 10px; top: 10px; z-index: 500; padding-left: 20px; padding-right: 20px; line-height: 30px; border-radius: 30px; font-size: 15px; color: #fff; background-color:rgba(15, 15, 15, 0.3);}
import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)function getCity(){ let defaultCity = "武汉"; if(localStorage.getItem("city")){ defaultCity = localStorage.getItem("city") } return defaultCity}export default new Vuex.Store({ state: { isLoading:true, city:getCity() }, mutations: { toggleCity(state,city){ state.city = city; } }, actions: { changeCity(ctx,city){ ctx.commit("toggleCity",city) localStorage.setItem("city",city) } }, modules: { }})