当前位置:网站首页>PC e-commerce platform - search module
PC e-commerce platform - search module
2022-06-21 21:04:00 【Front end young master Jia】
Catalog
Array methods splice And split
vuex Modularity in
vuex Use the trilogy
Write requests
page dispatch distributed actions Save to vuex in (actions Send the request commit notice mutations ,mutations Modify warehouse data )
Page fetching data ( Auxiliary function mapState Map to calculated properties )
Big warehouse
import Vue from 'vue'
import Vuex from 'vuex'
import home from './home'
import search from './search'
// You need to use plug-ins
Vue.use(Vuex)
// Object exposes a store An instance of a class
export default new Vuex.Store({
modules: {
home,
search
}
})
Small warehouse
import axios from '@/api'
// home Small warehouse of components
const state = {
// The default initial value depends on the server data format
CategoryList: []
}
const mutations = {
CategoryList(state, payload) {
state.CategoryList = payload
}
}
const actions = {
// adopt api Interface to get data
async AstncCategoryList({ commit }) {
let { data, code } = await axios.CategoryList();
if (code !== 200) return
commit('CategoryList', data)
}
}
const getters = {}
export default {
state,
mutations,
actions,
getters
}mount
// Import warehouse
import store from './store'
new Vue({
render: h => h(App),
// There will be one more on the component store Properties of
store
}).$mount('#app')
page
state Central sub module The calculation property is mapped to the page variable
import { mapState } from "vuex";
computed: {
...mapState({
// What you need on the right is a function , When using this calculation property , The function on the right will execute once immediately
// Into a state
CategoryList: (state) => state.home.CategoryList,
}),
},actions No modules
mounted() {
// notice vuex Send the request , The acquired data is stored in vuex in
this.$store.dispatch("AstncCategoryList");
},getters For simplifying data
// Compute properties Simplified data
const getters = {
// Of the current warehouse state
goodsList(state) {
// In case the network delays returning undefined
return state.SearchList.goodsList || []
}
}Object.assign() usage
1.Object.assign() Method is used to assign the values of all enumerable properties from one or more source objects to the target objects . It will return the target object .
2.Object.assign The method is shallow copy , Not a deep copy . The copy of the target object will get the reference of this object
3. grammar :Object.assign(target, …sources)
let aaa = {
text: 2,
value: 11,
}
let bbb = {
text: 3
}
let ccc = Object.assign(aaa,bbb) // aaa Target audience , bbb Source object
console.log(aaa) //3 11
console.log(bbb) //3
console.log(ccc) //3 11
summary :Object.assign() When merging objects , If the same key name exists , Values are different , Then the merged value (ccc) The value of this key is the source object (bbb) Value , And the target object (aaa) The value of
watch monitor
This is the problem of asynchronous requests , For example, we request a data asynchronously , However, the main thread is still running normally , When we hear the data request coming , Have the data , Then you can do other operations , This is the meaning of monitoring
watch There are several properties in monitoring :immediate,deep and handler
watch: {
// monitor carouselList Status changes
carouselList: {
// Deep monitoring
deep:true
// Monitor now
immediate:true
// Listen to the carouselList The state has changed
handler((newValue, oldValue){
// The next operation after listening to the data
}
}
}Array methods splice And split
splice
splice() The main purpose of is to insert items into the middle of an array , There are three ways :
1、 Delete : You can delete any number of items , You only need to specify two parameters : The location of the first item to delete and the number of items to delete .
2、 Insert : You can insert any number of items... Into a specified location , Just provide three parameters : The starting position 、0( Number of items to delete )、 And the item to insert .
3、 Replace : You can insert any number of items... Into a specified location , Delete any number of items at the same time , Specify three parameters : The starting position 、 Number of items to delete 、 And any number of items to insert .
splice() Method always returns an array , The array contains the items deleted from the original array ( If nothing is deleted , Returns an empty array )
Code display method :
var colors =["red","green","blue"];
var removed = colors.splice(0,1);// Delete the first item
console.log(colors);
console.log(removed);
removed = colors.splice(1,0,"yellow","blank");// From the position 1 Insert two
console.log(colors);
console.log(removed);
removed = colors.splice(1,1,"yellow","blank");// Insert two , Replace with
console.log(colors);
console.log(removed);
————————————————
[ 'red' ]
[ 'green', 'yellow', 'blank', 'blue' ]
[]
[ 'green', 'yellow', 'blank', 'blank', 'blue' ]
[ 'yellow' ]split() Method
split() Method is used to put a character string Split into string arrays .
separator Parameters : Required . String or Regular expressions , Split from where specified by this parameter stringObject.
howmany Parameters : Optional . This parameter specifies the maximum length of the returned array . If this parameter is set , No more substrings will be returned than the array specified by this parameter . If the parameter is not set , The entire string will be split , Regardless of its length
"2:3:4:5".split(":")
// Will return ["2", "3", "4", "5"]
"|a|b|c".split("|")
// Will return ["", "a", "b", "c"]"hello".split("")
// You can go back to ["h", "e", "l", "l", "o"], Split words
"hello".split("", 3)
// You can go back to ["h", "e", "l"], Take the first three letters of the word Array weight removal
utilize set Unique characteristics of structure member values
var arr = [100, 20, 30, 50, 30, 100, 20, 50];//[100,20,30,50]
var arrSet = new Set(arr);
//arrSet That is, an array without repeating elements // Traverse the old array , Then take the old array to query the new array , If the element does not appear in the new array , Let's add , Otherwise, do not add
// With the new array indexOf( Array elements ), If the return is -1 It means that there is no such element in the new array
function unique(arr){
var newarr = [];
for(i=0;i<arr.length;i++){
if(newarr.indexOf(arr[i]) === -1){
newarr.push(arr[i]);
}
}
return newarr;
}
var arr1 = unique(['a','v','a']);
console.log(arr1)
Custom pager
- The e-commerce platform shows a lot of data at the same time, reducing server pressure by using paging
- We need to know how many we are now pageNo Field , Represents the current number of pages
- How many pieces of data does each need to display pageSize character
- How many pieces of data are there total 【 You can see how many pages the pager has 】
- Number of consecutive pages :5|7【 Odd number 】continues
@getPageNo="getPageNo" Get the current number of pages
a key : Calculation start End consecutive page numbers ( Add... In the middle of the current page 2 Or minus 2 )
<template>
<div class="pagination">
<button :disabled="pageNo == 1" @click="$emit('getPageNo', pageNo - 1)">
The previous page
</button>
<button
v-if="statrtNumAndEndNum.start > 1"
@click="$emit('getPageNo', 1)"
:class="{ active: pageNo == 1 }"
>
1
</button>
<button v-if="statrtNumAndEndNum.start > 2">···</button>
<!-- v-for You can traverse objects Numbers character string Array
Number of page numbers at the end of traversal ( As long as it is greater than or equal to the number of starting pages, other hidden pages )
-->
<button
v-for="(page, index) in statrtNumAndEndNum.end"
:key="index"
@click="$emit('getPageNo', page)"
:class="{ active: pageNo == page }"
v-if="page >= statrtNumAndEndNum.start"
>
{
{ page }}
</button>
<button v-if="statrtNumAndEndNum.end < totalPage - 1">···</button>
<button
v-if="statrtNumAndEndNum.end < totalPage"
@click="$emit('getPageNo', totalPage)"
:class="{ active: pageNo == totalPage }"
>
{
{ totalPage }}
</button>
<button
:disabled="pageNo == totalPage"
@click="$emit('getPageNo', pageNo + 1)"
>
The next page
</button>
<button style="margin-left: 30px"> common {
{ total }} strip </button>
</div>
</template>
<script>
//:pageNo="1" Current page
//:pageSize="3" // How many on a page
//:total="91" // How many in all
//:continues="5" // Number of consecutive pages
export default {
name: "pagination",
props: ["pageNo", "pageSize", "total", "continues"],
computed: {
// Calculate how many pages
totalPage() {
// Round up to calculate how many pages
return Math.ceil(this.total / this.pageSize);
},
// Calculate the number of consecutive pages
statrtNumAndEndNum() {
const { continues, pageNo, totalPage } = this;
// Store the start and end numbers
let start = 0,
end = 0;
// The number of consecutive pages must be at least 5( The only special case is 4 page )
if (continues > totalPage) {
start = 1;
end = totalPage;
} else {
// The total number of pages must be greater than 5
// For example, the current is 8 Continuous for 5 // 6-7 8 9-10 5(8-5/2=8-2=6)
// For example, the current is 8 Continuous for 7 // 5-6-7 8 9-10-11(8-3=5)
start = pageNo - parseInt(continues / 2);
end = pageNo + parseInt(continues / 2);
// exclude start by 0 Or negative
if (start < 1) {
start = 1;
end = continues;
}
// Exclude cases that exceed the total number of pages
if (end > totalPage) {
end = totalPage;
// If it is 31 page (27-28-29-30 31)
// If it is 29 page (27-28-29 30 31)
start = totalPage - continues + 1; //31-5+1
}
}
return { start, end };
},
},
};
</script>
<style lang="less" scoped>
.pagination {
text-align: center;
button {
margin: 0 5px;
background-color: #f4f4f5;
color: #606266;
outline: none;
border-radius: 2px;
padding: 0 4px;
vertical-align: top;
display: inline-block;
font-size: 13px;
min-width: 35.5px;
height: 28px;
line-height: 28px;
cursor: pointer;
box-sizing: border-box;
text-align: center;
border: 0;
&[disabled] {
color: #c0c4cc;
cursor: not-allowed;
}
&.active {
cursor: not-allowed;
background-color: #409eff;
color: #fff;
}
}
.active {
background: skyblue;
}
}
</style>边栏推荐
- Introduction to high performance intranet DNS system
- 全局负载均衡实现原理
- The 17th National University RT thread innovation special award
- Fanuc robot carries out all backup, image backup and specific operations of loading backup files (picture and text)
- Extend the clean, fresh and dense bag, and put a "safety lock" on the ingredients
- Some shaders in AB package do not trigger the callback of ipreprocessshaders
- Analysis of ${} string splicing in JS
- 什么比国内现货白银更有优势?
- MySQl学习(从入门到精通 1.2)
- FANUC机器人进行全部备份和镜像备份以及加载备份文件的具体操作(图文)
猜你喜欢

Unity analog flashlight light source detector, AI attack range detection area, object detection in visual cone, fan-shaped area detection, circular area detection, cone area detection

4.3寸触摸屏智能网络中控主机有哪些应用

机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】

什么比国内现货白银更有优势?

MySQl学习(从入门到精通 1.2)

FM5012D小风扇集成ic方案

The difference between break and continue

What is more advantageous than domestic spot silver?

拼多多618手机品牌官旗销量同比增长124%,4000+高价位手机同比增长156%
![[专利与论文-20]:江苏省南京市2022年电子信息申报操作指南](/img/bb/313f4a9f9c03ab2e9dfe0e8846831e.png)
[专利与论文-20]:江苏省南京市2022年电子信息申报操作指南
随机推荐
行业首家!极氪APP获中国网络安全审查技术与认证中心权威认证
Flutter 输入框组件
FM5012D小风扇集成ic方案
拼多多618手机品牌官旗销量同比增长124%,4000+高价位手机同比增长156%
History of the Great Game
evaluating expression ‘ew.sqlSegment != null and ew.sqlSegment != ‘‘ and ew. mybaties plus问题
Extend the clean, fresh and dense bag, and put a "safety lock" on the ingredients
Decision tree learning notes
浅析Js中${}字符串拼接
高考后网上查询信息,注意防范没有 SSL证书的网站
Check information on the Internet after the college entrance examination, and pay attention to prevent websites without SSL certificates
volatile
Most detailed collation of vector basis of STL
NS32F103VBT6软硬件替代STM32F103VBT6
运维监控数据可视化-让数据自己会说话[华汇数据]
Visualization of operation and maintenance monitoring data - let the data speak [Huahui data]
SQL教程之6种鲜为人知的 SQL 技术,可帮助您每月节省 100 小时
函数的声明方式
MediaCodec的数据类型和使用方式
Servlet usage