请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,我在Home.vue中里边加入vuex的mapState,映射了city属性,但是页面报错了

报错图片描述
代码如下:

<template>
  <div>
    <home-header></home-header><!--:city="city"-->
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekend :list="weekendList"></home-weekend>
  </div>

</template>

<script>
import HomeHeader from './components/Header.vue'
import HomeSwiper from './components/Swiper.vue'
import HomeIcons from './components/Icons.vue'
import HomeRecommend from './components/Recomment.vue'
import HomeWeekend from './components/Weekend.vue'
import axios from 'axios'
import {
  mapState
} from 'vuex'
export default{
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  computed: {
    ...mapState(['city'])
   
  },
  data () {
    return {
      // city: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  methods: {
    getHomeInfo: function () {
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      // console.log('res:' + JSON.stringify(res))
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.city = data.city
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    console.log('mounted')
    this.getHomeInfo()
  },
  activated () {
    console.log('activated')
  }
}
</script>

<style scoped>

</style>

正在回答 回答被采纳积分+3

2回答

提问者 Keily 2021-01-01 12:37:56

我在换了种写法:

computed: {
    ...mapState({
      'cityFromStore': 'city' // 将vuex中的state中city属性映射到 计算属性city中
    }),
    city: {
      get () {
        return this.cityFromStore
      },
      set (newName) {
        return newName
      }
    }
  },


1 回复 有任何疑惑可以回复我~
提问者 Keily 2021-01-01 12:09:38

后来我换了个写法了,直接使用this.$store.state.city属性了;

1 回复 有任何疑惑可以回复我~
  • Dell #1
    这块用mapState,如果你给city 值,不能直接改,要用set,你代码里this.city = data.city 这块有问题
    回复 有任何疑惑可以回复我~ 2021-01-02 11:25:02
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信