fix(utils): unwrap refs when stringifying values in template

close #12884
close #12888
This commit is contained in:
Evan You 2023-12-06 16:38:16 +08:00
parent de0b97b3ea
commit ae3e4b1c70
2 changed files with 20 additions and 1 deletions

View File

@ -90,10 +90,18 @@ export function toString(val: any): string {
return val == null return val == null
? '' ? ''
: Array.isArray(val) || (isPlainObject(val) && val.toString === _toString) : Array.isArray(val) || (isPlainObject(val) && val.toString === _toString)
? JSON.stringify(val, null, 2) ? JSON.stringify(val, replacer, 2)
: String(val) : 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. * Convert an input value to a number for persistence.
* If the conversion fails, return original string. * If the conversion fails, return original string.

View File

@ -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))
})