數(shù)據(jù)可以請(qǐng)求到,但是可能是速度慢導(dǎo)致一開始undefined,這個(gè)怎么解決呢
store
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 :[];
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。