Skip to content

Commit

Permalink
fix(utils): unwrap refs when stringifying values in template
Browse files Browse the repository at this point in the history
close #12884
close #12888
  • Loading branch information
yyx990803 committed Dec 6, 2023
1 parent de0b97b commit ae3e4b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ export function toString(val: any): string {
return val == null
? ''
: Array.isArray(val) || (isPlainObject(val) && val.toString === _toString)
? JSON.stringify(val, null, 2)
? JSON.stringify(val, replacer, 2)
: String(val)
}

function replacer(_key: string, val: any): any {
// avoid circular deps from v3
if (val && val.__v_isRef) {
return val.value
}
return val
}

/**
* Convert an input value to a number for persistence.
* If the conversion fails, return original string.
Expand Down
11 changes: 11 additions & 0 deletions test/unit/modules/util/toString.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { toString } from 'core/util/index'
import { ref } from 'v3'

test('should unwrap refs', () => {
expect(
toString({
a: ref(0),
b: { c: ref(1) }
})
).toBe(JSON.stringify({ a: 0, b: { c: 1 } }, null, 2))
})

0 comments on commit ae3e4b1

Please sign in to comment.