Reputation: 218
I'm trying to send a function to a parent component through an event, but when i try to declare the variable that will store the function inside 'data(){return: }' the function gets executed as soon as the variable receives it. I need to find a place to declare the variable without executing the function inside it automatically.
//child
export default: {
data() {
return {
submit_method: { type: Function }
}
},
methods: {
open(type) {
if(type == 'newFolder') {
this.$refs.newFolder.show()
this.submit_method = this.exportFile()
} else {
this.$refs.olderOmbudsman.show()
}
},
exportFile() {
if ( this.doc.origin.id != null ) {
window.open(`${window.globals.API_SERVER}/ombudsman/doc/?page=${this.doc.page}&origin=${this.doc.origin.id}`, '_target')
}else{
alert("Choose the origin!");
}
}
},
activated() {
this.$emit('export-ombudsman', submit_method)
}
}
Upvotes: 0
Views: 454
Reputation: 29092
The first oddity I notice is this:
return {
submit_method: { type: Function }
}
Props support types, data
does not. All you're doing here is assigning the object {type: Function}
to submit_method
.
Then there's this:
this.submit_method = this.exportFile()
This is invoking the method immediately. I assume you meant this:
this.submit_method = this.exportFile
Then we've got this:
this.$emit('export-ombudsman', submit_method)
In templates you need to drop the this.
for accessing members but you can't do it in your JavaScript. This needs to be:
this.$emit('export-ombudsman', this.submit_method)
All of which assumes that submit_method
is dynamic. If it isn't then you can just pass exportFile
directly:
this.$emit('export-ombudsman', this.exportFile)
Even if you do need the function to be dynamic there isn't any need for submit_method
to be declared in your data
unless you need it to be reactive. You can still save it to this
even if it isn't in data
.
Upvotes: 1