Skip to content

computed implement toString #1257

@zhangenming

Description

@zhangenming

What problem does this feature solve?

const person = reactive({ firstName: "John", lastName: "Doe" });  // reactive field
const fullName = computed(() => `${person.firstName} ${person.lastName}`); // effect


console.log(`You are logged as: ${fullName.value}`); // Yo

What does the proposed API look like?

console.log(`You are logged as: ${fullName}`); // Yo

maybe code change

  computed = {
    __v_isRef: true,
    // expose effect so computed can be stopped
    effect: runner,
    get value() {
      if (dirty) {
        value = runner()
        dirty = false
      }
      track(computed, TrackOpTypes.GET, 'value')
      return value
    },
    set value(newValue: T) {
      setter(newValue)
    },
    toString() {
      return this.value // THIS
    },
  } as any

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions