鍍金池/ 問答/HTML/ vue 請(qǐng)求數(shù)據(jù)報(bào)undefined

vue 請(qǐng)求數(shù)據(jù)報(bào)undefined

數(shù)據(jù)可以請(qǐng)求到,但是可能是速度慢導(dǎo)致一開始undefined,這個(gè)怎么解決呢
clipboard.png

clipboard.png

store

clipboard.png

import Util from "../../util/common";
import * as types from "../types";
import Vue from "vue";

// 容器
const state = {
  productDatas: "", //detail父組件請(qǐng)求的當(dāng)前頁面商品數(shù)據(jù)
  colSelected: 0, //0是index,表示第一個(gè)
  sizeSelected: 0, //0是index,表示第一個(gè)
  count: 0, //購物車?yán)锏纳唐窋?shù)量
  carList: "", //購物車的商品列表
  fetchLoading: false, //全局加載狀態(tài)的Loading
  selectedList: "", //已選擇的購物車商品列表
  unSelectedList: "" //未選擇的購物車商品列表,提交訂單后用它替換carList
};

//更改 store 中的狀態(tài)的唯一方法:提交 mutation
/*
  購物車邏輯:
      unSelectedList:未打鉤的購物車商品列表
      SelectedList:已勾選的購物車商品列表
      carList:購物車商品列表
      當(dāng)支付成功之后,購物車列表需要減掉SelectedList,留下unSelectedList
      直接用unSelectedList替換當(dāng)前carList即可
*/
const mutations = {
  //異步請(qǐng)求的數(shù)據(jù)
  [types.SET_DATAS](state, res) {
    state.productDatas = res;
  },

  //詳情頁商品顏色的選擇
  [types.CHANGE_COL_SELECTED](state, res) {
    state.colSelected = res;
  },

  //詳情頁商品尺寸的選擇

  [types.CHANGE_SIZE_SELECTED](state, res) {
    state.sizeSelected = res;
  },

  // 向購物車商品列表添加商品
  [types.ADD_PRODUCT](state) {
    state.carList = Util.getLocal("carList");
  },

  //獲取當(dāng)前購物車商品數(shù)量
  [types.CHANGE_COUNT](state) {
    state.count = Util.getLocal("count");
  },

  // 重置購物車
  [types.RESET_CARLIST](state) {
    state.carList = Util.getLocal("carList");
  },

  // 重置購物車數(shù)量
  [types.RESET_COUNT](state) {
    state.count = Util.getLocal("carList").length;
  },

  // loading開關(guān)
  [types.SET_LOADING](state, res) {
    state.fetchLoading = res;
  },
  // 購物車?yán)锎蜚^的商品
  ["SET_SELECTEDLIST"](state, res) {
    state.selectedList = Util.getLocal("selectedList");
  },

  //購物車?yán)餂]打鉤的商品

  ["SET_UNSELECTEDLIST"](state) {
    state.unSelectedList = Util.getLocal("unSelectedList");
  }
};

let vm = new Vue({});

// action提交mutations,不直接更改狀態(tài)(通過store.dispatch觸發(fā))
const actions = {
  // 父組件發(fā)送異步請(qǐng)求
  setDatas({ commit }) {
    let params={
      goodsId :"0d2420561a1b4360ad5f6a73067f0510",
      userId:''
    }
    vm.$fetch.api_detail
      .product(params)
      .then(({ data }) => {
        console.log(data.data)
        commit('SET_DATAS', data.data);
      })
      .catch(() => {});
    //  vm.$api({
    //      method:'post',
    //      url:"/detail"
    //    }).then(response=>{
    //      commit('SET_DATAS',response.data);
    //    })
  },

  // 購物車數(shù)量增減,true是加,false是減
  setLocalCount({ commit }, bool = true) {
    let count = Util.getLocal("count") || 0;
    if (bool) {
      Util.setLocal(++count, "count");
    } else {
      Util.setLocal(--count, "count");
    }
    commit(types.CHANGE_COUNT);
  },

  //網(wǎng)購物車列表添加數(shù)據(jù)
  addCarList({ commit }, res) {
    Util.setLocal(res, "carList", true);
    commit(types.ADD_PRODUCT);
  },

  //重新設(shè)置購物車商品列表,把打鉤并提交的商品去掉,即保留unSelectedList

  resetCarList({ commit, getters }) {
    const unSelectedList = Util.getLocal("unSelectedList");
    Util.setLocal(unSelectedList, "carList");
    commit(types.RESET_CARLIST);
  },
  // 重置購物車數(shù)量Count,即沒打鉤的商品的數(shù)量
  resetCount({ commit, getters }) {
    const count = Util.getLocal("unSelectedList").length;
    Util.setLocal(count, "count");
    commit(types.RESET_COUNT);
  },

  // 刪除購物車列表的某一項(xiàng) (用新的數(shù)組直接替換)
  cutCarList({ commit }, res) {
    Util.setLocal(res, "carList");
    commit(types.RESET_CARLIST);
  },

  // 分別保存打鉤的商品和為打鉤的商品
  setSelectedList({ commit, getters }) {
    Util.setLocal(getters.selectedList, "selectedList");
    commit("SET_SELECTEDLIST");
    Util.setLocal(getters.unSelectedList, "unSelectedList");
    commit("SET_UNSELECTEDLIST");
  }
};

// 如同計(jì)算屬性,處理state的某個(gè)狀態(tài)
const getters = {
  selectedList(state) {
    // choseBool為真的商品 即打鉤的商品
    if (state.carList !== "") {
      let arr = state.carList.filter(ele => {
        return ele.choseBool == true;
      });
      return arr;
    }
  },

  unSelectedList(state) {
    if (state.carList !== "") {
      let arr = state.carList.filter(ele => {
        return ele.choseBool == false;
      });
      return arr;
    }
  }
};

export default {
  state,
  actions,
  getters,
  mutations
};

詳情頁vue

<template lang="html">
  <div class="detail">
<div class="header">

  <mt-navbar v-model="selected">
    <mt-tab-item id="1">商品</mt-tab-item>
    <mt-tab-item id="2">庫存</mt-tab-item>
  </mt-navbar>
</div>
<!-- tab-container -->
<mt-tab-container v-model="selected">
  <mt-tab-container-item id="1">
   <v-swiper> </v-swiper>
    <v-chose></v-chose>
    <v-content></v-content>
    <v-baseline></v-baseline>
    <v-footer></v-footer>
  </mt-tab-container-item>
  <mt-tab-container-item id="2">
    <mt-cell v-for="n in 4" :title="'測(cè)試 ' + n" />
  </mt-tab-container-item>

</mt-tab-container>
   
  </div>
</template>

<script>
import Swiper from "@/components/detail/swiper.vue";
import Chose from "@/components/detail/chose.vue";
import Content from "@/components/detail/content.vue";
import Footer from "@/components/detail/footer.vue";
import Baseline from "@/common/_baseline.vue";
import detail from "@/http/mock.js"; //模擬數(shù)據(jù)
export default {
  components: {
    "v-swiper": Swiper,
    "v-chose": Chose,
    "v-content": Content,
    "v-footer": Footer,
    "v-baseline": Baseline
  },
  data() {
    return {
      selected: "1"
    };
  },
  beforeCreate() {
    this.$store.dispatch("setDatas");
  }
};
</script>

<style lang="less" scoped>
.detail {
  width: 100%;
  padding-bottom: 14vw;
}
.mint-navbar {
  width:100px;
  margin:0 auto;
}
.mint-navbar .mint-tab-item.is-selected {
    border-bottom: 3px solid #333333;
    color: #333333;
    margin-bottom: 0;
}
</style>

里面引用swiper

====================華麗麗的分割線=======================

一開始判斷的this.$store.state.detail.productDatas.goodsDetails.imgsList是否為undefined

不能判斷imgsList是否是undefined,因?yàn)樯霞?jí)的是goodsDetails也不存在

感謝大神的回答!修改了一下解決問題了

 swiper() { 
       const {productDatas } = this.$store.state.detail;
       return productDatas ? this.$store.state.detail.productDatas.goodsDetails.imgsList :[];
    
    }
回答
編輯回答
舊螢火

store里的state初始值里沒有那些屬性所以報(bào)undefined,在computed里 判斷下underfined時(shí) return 空數(shù)組
comptued:{

swiper(){
    const { productsdata }= this.store.state;
    return productsdata ?你的代碼 :【】;
}


大致這樣 手機(jī)
你那個(gè)state.detail是哪里來的

2017年12月26日 18:28