readonly() Create a Readonly Proxy

Last updated: 2026-10-22

readonly() Create a Readonly Proxy

readonly() creates a readonly proxy of the source: writes warn and fail, protecting data from accidental mutation.

GroupReactivity: Core
KindAPI
ScopeComponent
Vue Version3.0+
SSR SupportYes

📝 Syntax

const config = readonly({ theme: 'dark' })
config.theme = 'light'  // ❌ warns: write operation failed

⚙️ Arguments

targetAn object or a ref (a readonly ref is returned for refs)

Returns:A deep readonly proxy

▶ Example

Edit the code below and see the live result:

Output:

Theme stays dark; clicking logs a failed-write warning and nothing changes.

💡 Key Points

💡 Tip Typical use: wrap config passed to children so they cannot mutate the parent's data.
ℹ️ Note The direction is one-way: source changes propagate in, writes are blocked.
⚠️ Warning Warnings appear in dev and writes fail silently in production — do not use it as runtime validation.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Does readonly prevent every mutation?
A It traps writes through the proxy; mutating the original directly still works, so it is not immutability.
Q Does a readonly proxy still update?
A Yes — source changes propagate through; only writes are blocked.
Q How is it different from Object.freeze?
A freeze mutates the real object irreversibly; readonly is a proxy layer that keeps reactivity.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏