Home / Function/ computed() — vue Function Reference

computed() — vue Function Reference

Architecture documentation for the computed() function in computed.ts from the vue codebase.

Entity Profile

Dependency Diagram

graph TD
  010c36af_5e76_9aa0_d906_6eada399f824["computed()"]
  86936286_63e2_eb4f_1b26_6a4b4026b810["evaluate()"]
  010c36af_5e76_9aa0_d906_6eada399f824 -->|calls| 86936286_63e2_eb4f_1b26_6a4b4026b810
  d1eb6d83_f4fb_7e95_6bf0_bea3d8b0996f["depend()"]
  010c36af_5e76_9aa0_d906_6eada399f824 -->|calls| d1eb6d83_f4fb_7e95_6bf0_bea3d8b0996f
  style 010c36af_5e76_9aa0_d906_6eada399f824 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/v3/reactivity/computed.ts lines 37–100

export function computed<T>(
  getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
  debugOptions?: DebuggerOptions
) {
  let getter: ComputedGetter<T>
  let setter: ComputedSetter<T>

  const onlyGetter = isFunction(getterOrOptions)
  if (onlyGetter) {
    getter = getterOrOptions
    setter = __DEV__
      ? () => {
          warn('Write operation failed: computed value is readonly')
        }
      : noop
  } else {
    getter = getterOrOptions.get
    setter = getterOrOptions.set
  }

  const watcher = isServerRendering()
    ? null
    : new Watcher(currentInstance, getter, noop, { lazy: true })

  if (__DEV__ && watcher && debugOptions) {
    watcher.onTrack = debugOptions.onTrack
    watcher.onTrigger = debugOptions.onTrigger
  }

  const ref = {
    // some libs rely on the presence effect for checking computed refs
    // from normal refs, but the implementation doesn't matter
    effect: watcher,
    get value() {
      if (watcher) {
        if (watcher.dirty) {
          watcher.evaluate()
        }
        if (Dep.target) {
          if (__DEV__ && Dep.target.onTrack) {
            Dep.target.onTrack({
              effect: Dep.target,
              target: ref,
              type: TrackOpTypes.GET,
              key: 'value'
            })
          }
          watcher.depend()
        }
        return watcher.value
      } else {
        return getter()
      }
    },
    set value(newVal) {
      setter(newVal)
    }
  } as any

  def(ref, RefFlag, true)
  def(ref, ReactiveFlags.IS_READONLY, onlyGetter)

  return ref
}

Domain

Subdomains

Frequently Asked Questions

What does computed() do?
computed() is a function in the vue codebase.
What does computed() call?
computed() calls 2 function(s): depend, evaluate.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free