數(shù)據(jù)可以請求到,但是可能是速度慢導致一開始undefined,這個怎么解決呢
store
import Util from "../../util/common";
import * as types from "../types";
import Vue from "vue";
// 容器
const state = {
productDatas: "", //detail父組件請求的當前頁面商品數(shù)據(jù)
colSelected: 0, //0是index,表示第一個
sizeSelected: 0, //0是index,表示第一個
count: 0, //購物車里的商品數(shù)量
carList: "", //購物車的商品列表
fetchLoading: false, //全局加載狀態(tài)的Loading
selectedList: "", //已選擇的購物車商品列表
unSelectedList: "" //未選擇的購物車商品列表,提交訂單后用它替換carList
};
//更改 store 中的狀態(tài)的唯一方法:提交 mutation
/*
購物車邏輯:
unSelectedList:未打鉤的購物車商品列表
SelectedList:已勾選的購物車商品列表
carList:購物車商品列表
當支付成功之后,購物車列表需要減掉SelectedList,留下unSelectedList
直接用unSelectedList替換當前carList即可
*/
const mutations = {
//異步請求的數(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");
},
//獲取當前購物車商品數(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開關
[types.SET_LOADING](state, res) {
state.fetchLoading = res;
},
// 購物車里打鉤的商品
["SET_SELECTEDLIST"](state, res) {
state.selectedList = Util.getLocal("selectedList");
},
//購物車里沒打鉤的商品
["SET_UNSELECTEDLIST"](state) {
state.unSelectedList = Util.getLocal("unSelectedList");
}
};
let vm = new Vue({});
// action提交mutations,不直接更改狀態(tài)(通過store.dispatch觸發(fā))
const actions = {
// 父組件發(fā)送異步請求
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);
},
//網購物車列表添加數(shù)據(jù)
addCarList({ commit }, res) {
Util.setLocal(res, "carList", true);
commit(types.ADD_PRODUCT);
},
//重新設置購物車商品列表,把打鉤并提交的商品去掉,即保留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);
},
// 刪除購物車列表的某一項 (用新的數(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");
}
};
// 如同計算屬性,處理state的某個狀態(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="'測試 ' + 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,因為上級的是goodsDetails也不存在
感謝大神的回答!修改了一下解決問題了
swiper() {
const {productDatas } = this.$store.state.detail;
return productDatas ? this.$store.state.detail.productDatas.goodsDetails.imgsList :[];
}北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產業(yè)為響應國家深化產教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產業(yè)鏈。利用北京大學優(yōu)質教育資源及背
博為峰,中國職業(yè)人才培訓領域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經理從事移動互聯(lián)網管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經理職務負責iOS教學及管理工作。
浪潮集團項目經理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網頁制作和網頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經驗。曾經歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。