Skip to content

Commit

Permalink
fix: debounce和throttle改为lodash-es引入 mv页面的route props选项中把id由string转为nu…
Browse files Browse the repository at this point in the history
…mber
  • Loading branch information
sl1673495 committed Sep 26, 2019
1 parent 9ba4804 commit e07f60e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"core-js": "^2.6.5",
"element-ui": "^2.10.1",
"good-storage": "^1.1.0",
"lodash-es": "^4.17.15",
"vue": "^2.6.10",
"vue-meta": "^2.2.1",
"vue-router": "^3.0.3",
Expand Down
7 changes: 6 additions & 1 deletion src/page/mv/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ import MvCard from "@/components/mv-card"
export default {
mixins: [hideMenuMixin],
props: ["id"],
props: {
id: {
type: Number,
required: true
}
},
metaInfo() {
return {
title: this.mvDetail.name
Expand Down
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default new Router({
path: '/mv/:id',
name: 'mv',
component: Mv,
props: true,
props: (route) => ({id: +route.params.id}),
},
...menuRoutes,
],
Expand Down
37 changes: 2 additions & 35 deletions src/utils/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Notification } from 'element-ui'

export { debounce, throttle } from 'lodash-es'

export function pad(num, n = 2) {
let len = num.toString().length
while (len < n) {
Expand Down Expand Up @@ -60,41 +62,6 @@ export function genImgUrl(url, w, h) {
return url
}

export function debounce(fn, delay) {
let timer = null

return function () {
const args = arguments
const context = this

if (timer) {
clearTimeout(timer)

timer = setTimeout(function () {
fn.apply(context, args)
}, delay)
} else {
timer = setTimeout(function () {
fn.apply(context, args)
}, delay)
}
}
}

export function throttle(action, delay) {
let last = 0
// 节流结束后需要执行一次校正
// 利用debouce即可
const lastTimeAction = debounce(action, delay)
return function () {
const curr = +new Date()
if (curr - last > delay) {
action.apply(this, arguments)
last = curr
}
lastTimeAction()
}
}

export function isLast(index, arr) {
return index === arr.length - 1
Expand Down

0 comments on commit e07f60e

Please sign in to comment.