步骤
- 拆分静态组件(略)
写 Api 接口
import axios from "axios";
import nprogress from 'nprogress';
import "nprogress/nprogress.css"
const requests = axios.create({
baseURL:"/api",
timeout:5000,
});
requests.interceptors.request.use((config)=>{
nprogress.start();
return config;
});
requests.interceptors.response.use(
(res)=>{
nprogress.done();
return res.data;
},
(error)=>{
return Promise.reject(new Error('faile'));
})
export default requests;
......
export const reqGetSearchInfo = (params) => requests({
url: '/list',
method:'post',
data:params
})
vuex三连招
import { reqGetSearchInfo } from "@/api"
const state = {
searchList: {}
}
const mutations = {
GETSEARCHLIST(state,data){
state.searchList = data;
}
}
const actions = {
async getSearchList({ commit }, params = {}) {
let result = await reqGetSearchInfo(params);
if (result.code == 200) {
commit("GETSEARCHLIST", result.data);
}
},
}
const getters = {
goodsList(state){
return state.searchList.goodsList || [];
},
trademarkList(state){
return state.searchList.trademarkList || [];
},
attrsList(state){
return state.searchList.attrsList || [];
}
}
export default {
state,
mutations,
actions,
getters
}
去 search 组件发请求并获取数据
<script>
import { mapGetters } from "vuex";
import SearchSelector from "./SearchSelector/SearchSelector";
export default {
name: "Search",
data() {
return {
searchParams: {
category1Id: "",
category2Id: "",
category3Id: "",
categoryName: "",
keyword: "",
order: "",
pageNo: 1,
pageSize: 10,
props: [],
trademark: "",
},
};
},
components: {
SearchSelector,
},
beforeMount() {
Object.assign(this.searchParams,this.$route.query,this.$route.params);
},
mounted(){
this.getData();
this.searchParams.category1Id='';
this.searchParams.category2Id='';
this.searchParams.category3Id='';
},
computed: {
...mapGetters(["goodsList"]),
},
methods: {
getData() {
this.$store.dispatch("getSearchList", this.searchParams);
},
},
watch: {
$route(newValue,oldValue){
Object.assign(this.searchParams,this.$route.query,this.$route.params);
this.getData();
this.searchParams.category1Id='';
this.searchParams.category2Id='';
this.searchParams.category3Id='';
}
}
};
</script>
展示动态数据(略)