I am passing a props to a component:
<template>
{{messageId}}
// other html code
</template>
<script>
export default {
props: ['messageId'],
data: function(){
var theData={
// below line gives ReferenceError messageId is not defined
somevar: messageId,
// other object attributes
}
}
}
</script>
In above code, I have commented the line that gives the error. If I remove that line, it works as normal and template renders properly (and I can see the expected value of {{messageId}} as well). Hence the logic to pass data is correct.
It seems that the way to access the messageId
in data() is wrong.
So how do I access the props messageId
in data?
To access props in a Vue. js component data function, we can get them from this . to register the messageId prop. Then we get the initial value of the messageId prop with this.
You can pass strings, arrays, numbers, and objects as props. But can you pass a function as a prop? While you can pass a function as a prop, this is almost always a bad idea. Instead, there is probably a feature of Vue that is designed exactly to solve your problem.
The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.
From the data()
method, you can reference the component's properties using this
.
So in your case:
data: function() {
var theData = {
somevar: this.messageId,
// other object attributes
}
return theData;
}
EDIT: According to Ryans posts it is possible to reference the instance with an arrow function like this:
data: (instance) => ({
somevar: instance.messageId
}),
PREVIOUS POST:
Note that this does not work if you are using an arrow function for assigning your data:
data: () => ({
somevar: this.messageId // undefined
}),
Because this
will not point to the component. Instead, use a plain function:
data: function() {
return { somevar: this.messageId }
},
or using ES6 object method shorthand as Siva Tumma suggested:
data() {
return { somevar: this.messageId }
}
To assign a data property equal to a props, you can use watcher, as following:
<script>
export default {
props: ['messageId'],
data: function(){
var theData={
somevar: "",
// other object attributes
}
},
watch: {
messageId: function(newVal) {
this.somevar = newVal
}
}
}
as @Saurabh described, I want to add more details.
If you are rendering a Child
component, and want to set the data
variables by props
, you have to use both this
and watch
functions:
this
to access the props
variables.<script>
export default {
data () {
id: 0
},
props: ['propId'],
methods: {
init() {
this.id = this.propId // step1. assign propId to id
}
}
}
</script>
props
variable<script>
export default {
data () {
id: 0
},
props: ['propId'],
methods: {
init() {
this.id = this.propId // step1. assign propId to id
}
},
// add this to your code , this is a MUST.
// otherwise you won't SEE your data variable assigned by property
watch {
propId: function(new_value) {
this.init()
}
}
}
</script>
Assuming you have two component: Parent
and Child
( Child
has a property naming as propId
) , and Child
also have a "async" operation such as reading database.
// Parent's View
<Child propId='parent_var_id'></Child>
3.1 Parent
got rendered
3.2 Child
got rendered , in this case, parent_var_id blank
3.3 10ms
later, parent_var_id changed to 100
3.4 Child
property propId
also changed as binding.
3.5 Child
's watch
function called, and the variable id
defined in data
changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With