Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR ACTIONSCRIPT

dynamic computed property vue

computed: {
  element() {
    return this.getElements(...);
  },
},
watch: {
  element: {
    handler: function (newValue) {
      if (newValue.inputName) {
        this.addProp(['element', 'inputName'], () => { return 'someValue' })
      }
    },
    deep: true
  }
},
methods: {
  addProp (path, getter) {
    // Get property reference or undefined if not (yet) valid
    const propName = path.reduce((acc, prop) => acc ? acc[prop] : undefined, this)
    if (!propName) { return }

    const computedProp = {
      get() {
        return getter()
      }
    }
    this[propName] = computedProp
  },
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #dynamic #computed #property #vue
ADD COMMENT
Topic
Name
5+2 =