I'm trying to display a string in reverse, using Vue. My template is:
<div id="app">
<reverse :msgreverse="message" :reverseMessage="reverseMessage()"></reverse>
</div>
And my script:
function reverseMessage(msg) {
return msg.split('').reverse().join('')
}
Vue.component('reverse', {
props:["msgreverse", "reverseMessage"],
template: '<p v-html="reverseMessage(msgreverse)"></p>'
})
var app = new Vue({
el: '#app',
data: {
message:'The message to reverse !',
}
})
This fails with the following console errors:
TypeError: reverseMessage is not a function. (In 'reverseMessage()', 'reverseMessage' is undefined)
Error in render: "TypeError: reverseMessage is not a function. (In 'reverseMessage()', 'reverseMessage' is undefined)"
Property or method "reverseMessage" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property
How do I get the <reverse>
component to display a given string in reverse?
reverseMessage
function has to be defined as your Vue instance method inside ofmethods: {}
. Check if that works. Also, isn't it better to define it in yourreverse
component and call it there (without the prop)? It's what's that component is for after all. – dziraf