1

I've already seen this answer which fits my question : In Vue JS, call a filter from a method inside the vue instance

Now with that out of the way, when doing

console.log(this.$options)

I get undefined so I can't call filters on it.. This is my code:

 methods:{
    style:(input)=>{
      return {
        backgroundColor:this.$options.filters.color(input),
      }
    }
  },
  filters: {
    color: function (value) {
      console.log(color(value));
      if (!value) return ''
      return `rgb(${value},${value},${value})`
    }
  }

Error in render: "TypeError: Cannot read property 'filters' of undefined"
2
  • 1
    I think may be your arrow function ! How about changing style: function(input) {} Commented Jul 24, 2018 at 7:53
  • you're absolutely right , figured it out in the end, thanks! Commented Jul 24, 2018 at 8:11

2 Answers 2

1

You are using the arrow function for style method. It should be:

style(input) {
    return {
        backgroundColor:this.$options.filters.color(input),
    }
}

And if you are not using this within your filter then you can extract it outside like:

function color (value) {
    console.log(color(value));
    if (!value) return ''
    return `rgb(${value},${value},${value})`
}

methods: {
    style(input) {
        return {
            backgroundColor: color(input),
        }
    }
},
filters: { color }
Sign up to request clarification or add additional context in comments.

1 Comment

the first part is the right answer even tho the second way of coding also makes sense thank you !
0

you can write this function in methods

methods:{
    style:(input)=>{
      return {
        backgroundColor:this.color(input),
      }
    },
    color: (value) {
      console.log(color(value));
      if (!value) return ''
        return `rgb(${value},${value},${value})`
      }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.