posva / vue-reactive-refs Goto Github PK
View Code? Open in Web Editor NEWMake $refs reactive so they can be used in computed properties and watchers
License: MIT License
Make $refs reactive so they can be used in computed properties and watchers
License: MIT License
Using @vue/compat, the beforeCreate hook in proxy.ts throws an exception:
this.$refs = new Proxy(...)
TypeError: 'set' on proxy: trap returned falsish for property
My Vue 3 fix is to change the hook to this:
beforeCreate() {
this.$.refs = reactive({});
}
Note the period between the '$' and the 'refs'.
I swore I read somewhere that $refs was going to be reactive in Vue 3, but it seems not to be. Maybe I just imagined it...
<template>
<div>
<input ref="input" />
<button v-for="button in actions" ref="buttons">{{ action }}</button>
<div>{{inputValue}}</div>
</div>
</template>
<script>
export default {
// this is necessary
refs: ['input', 'buttons'],
computed: {
// NOTE: this is definitely not what you should use this lib for, but it's
// the easiest example
inputValue() {
return this.$refs.input && this.$refs.input.value
},
// Same for this example. It's ALWAYS better to mapping your data (the source of truth)
// and avoid at all cost mapping information to the DOM
buttonsText() {
return this.$refs.buttons && this.$refs.buttons.map(b => b.innerText)
},
},
}
</script>
like this
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.