Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

Vue 2 Transition and Animation System

I recently used the Vue 2 transition and animation system in a project and found it more complex than expected. Here are some lessons from putting it into practice.

Quick Start ​

For a concrete implementation, see the following code:

css
function pLimit(concurrency) {
  const queue = []
  let active = 0

  const next = () => {
    if (active >= concurrency || queue.length === 0) return
    active++
    const { fn, resolve, reject } = queue.shift()
    fn().then(resolve, reject).finally(() => {
      active--
      next()
    })
  }

  return (fn) => new Promise((resolve, reject) => {
    queue.push({ fn, resolve, reject })
    next()
  })
}

This approach has been validated in production and runs stably.

Advanced Usage ​

Let's start with the basic usage:

css
class EventEmitter {
  constructor() {
    this.events = new Map()
  }

  on(event, handler) {
    if (!this.events.has(event)) {
      this.events.set(event, [])
    }
    this.events.get(event).push(handler)
    return () => this.off(event, handler)
  }

  off(event, handler) {
    const handlers = this.events.get(event)
    if (handlers) {
      const idx = handlers.indexOf(handler)
      if (idx > -1) handlers.splice(idx, 1)
    }
  }

  emit(event, ...args) {
    const handlers = this.events.get(event) || []
    handlers.forEach(h => h(...args))
  }
}

This approach is concise and clear, and works for most scenarios.

Business Scenarios ​

The core code looks like this:

css
interface User {
  id: number
  name: string
  email: string
  role: 'admin' | 'user' | 'guest'
}

function createUser(data: Partial<User>): User {
  return {
    id: Date.now(),
    name: data.name || '',
    email: data.email || '',
    role: data.role || 'user'
  }
}

type UserKeys = keyof User  // 'id' | 'name' | 'email' | 'role'

In real projects you also need to account for edge cases and exception handling.

Pitfall Avoidance Guide ​

Here is a concrete example:

css
function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key]
}

const user: User = { id: 1, name: '张三', email: 'a@b.com', role: 'admin' }
const name = pluck(user, 'name')   // string
const role = pluck(user, 'role')   // 'admin' | 'user' | 'guest'

After this pattern was rolled out across the team, the results were good and maintenance costs dropped noticeably.

Summary ​

  • When you hit a problem, read the source code and the official docs more often.
  • The key to the Vue 2 transition and animation system is understanding the core concepts, not just surface-level usage.
  • Choose the right approach for the scenario in real projects.

MIT Licensed