0

I need to use filter "capitalize" in vue js 2, but when I add filters according to documentation, it gave me error.

my code

        <template>
        <div class="test">
            <h1>{{title}}</h1>
            <ul>
                <li v-for="item in items | capitalize" :key="item.first">{{item.first}},{{item.last}}</li>
            </ul>
        </div>
        </template>


        <script>
        export default {
        name: 'test',
        vinput: '',
        data () {
            return {
            title: 'Hello,
            items: [
                {
                first: 'John0',
                last: 'Doe'
                },
                {
                first: 'Pavel',
                last: 'Doe'
                },
                {
                first: 'Shox',
                last: 'Doe'
                }
            ]
            }
        },
        filters: {
            capitalize: function (value) {
            if (!value) return ''
            value = value.toString()
            return value.charAt(0).toUpperCase() + value.slice(1)
            }
        }
        }
        </script>

error

enter image description here

Any ideas, how to corrext that error?

5
  • Need to show more code Commented Apr 6, 2018 at 9:32
  • is it ok , now? Commented Apr 6, 2018 at 9:48
  • You can't capitalize object, at least not with the filter function you wrote.In fact it accept string being passed.Your filter shall be used here {{item.first | capitalize}},{{item.last | capitalize}} Commented Apr 6, 2018 at 9:51
  • can you help to change this code, because I'm beginer it is a bit difficult for me Commented Apr 6, 2018 at 9:53
  • Possible duplicate of Filter list with Vue.js Commented Apr 6, 2018 at 10:40

2 Answers 2

1

Use filters in the template syntax.

<li v-for="item in items" :key="item.first">{{item.first | capitalize}},{{item.last | capitalize}}</li>
Sign up to request clarification or add additional context in comments.

Comments

0

First, we have to define in main.js as shown below.

Vue.filter('UpperCase', function(val) {
  return val.toUpperCase()
})

Then, we have to use where we need as shown below.

<div>{{ name | UpperCase }}</div>

Whatever function you want, just write function and call it.

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.