vue.js - Access Vue instance's computed properties object -
vue components exposes this.$data
. there way access computed properties in similar fashion?
they not exposed on $data
, , there no such thing this.$computed
there's no built-in way access object computed properties of vue instance.
if want object of computed property name , values testing purposes define own $computed
property using information in _computedwatchers
property. might finicky , wouldn't use in production code.
object.defineproperty(vue.prototype, '$computed', { get() { let computed = {}; object.keys(this._computedwatchers).foreach((key) => { computed[key] = this._computedwatchers[key].value; }) return computed; } }) new vue({ el: '#app', data() { return { foo: 1, } }, computed: { bar() { return this.foo * 2; } }, mounted() { console.log(this.$computed) } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script> <div id="app">{{ bar }}</div>
Comments
Post a Comment