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
  3c7c3201_1e02_c754_faab_86ffa29870a6["computed()"]
  49a97e06_4034_4e1d_a0e8_bb7368ceb3af["computed.ts"]
  3c7c3201_1e02_c754_faab_86ffa29870a6 -->|defined in| 49a97e06_4034_4e1d_a0e8_bb7368ceb3af
  style 3c7c3201_1e02_c754_faab_86ffa29870a6 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, defined in src/v3/reactivity/computed.ts.
Where is computed() defined?
computed() is defined in src/v3/reactivity/computed.ts at line 37.

Analyze Your Own Codebase

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

Try Supermodel Free