mirror of
https://github.com/vuejs/vue.git
synced 2024-11-22 04:39:46 +00:00
fix(utils): unwrap refs when stringifying values in template
close #12884 close #12888
This commit is contained in:
parent
de0b97b3ea
commit
ae3e4b1c70
@ -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.
|
||||||
|
11
test/unit/modules/util/toString.spec.ts
Normal file
11
test/unit/modules/util/toString.spec.ts
Normal 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))
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user