Vue 3 Uppercase Methods
November 4, 2024 ยท AI Conversations
### 1. Using `toUpperCase()` in the Template
If you only need to transform the text once, you can apply JavaScript's `toUpperCase()` directly in the template.
<template>
<div>{{ message.toUpperCase() }}</div>
</template>
<script>
export default {
data() {
return {
message: 'hello world'
};
}
};
</script>
### 2. Using a Computed Property
For a more organized approach, especially if you need to reuse the transformation, define a computed property.
<template>
<div>{{ uppercaseMessage }}</div>
</template>
<script>
export default {
data() {
return {
message: 'hello world'
};
},
computed: {
uppercaseMessage() {
return this.message.toUpperCase();
}
}
};
</script>
### 3. Using a Custom Filter (Global Option)
While Vue 3 removed filters from the core, you can create a custom filter using a method or a computed property.
#### Example with a method:
<template>
<div>{{ toUpperCase(message) }}</div>
</template>
<script>
export default {
data() {
return {
message: 'hello world'
};
},
methods: {
toUpperCase(text) {
return text.toUpperCase();
}
}
};
</script>
### 4. Using CSS for Display Purposes
If the change is purely visual and you don't need the actual data in uppercase, use CSS with the `text-transform` property:
<template>
<div class="uppercase">{{ message }}</div>
</template>
<script>
export default {
data() {
return {
message: 'hello world'
};
}
};
</script>
<style scoped>
.uppercase {
text-transform: uppercase;
}
</style>
Choose the method that best fits your use case and application structure.