Vue: passing onclick-method name via props





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to create components in a loop, passing properties to the child by name.



so:
<card-base v-for="(card, i) in cards" :key="i" :heading="card.heading" :width="card.width" class="dashboard-card" :cardHeaderButtons="[{icon: 'minimize', fn: 'minimizeDashboardCard'}]">



in my (child!) component i have defined the minimizeDashboardCard method,
and



                <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="button.fn"
>
<v-icon>
{{ button.icon }}
</v-icon>
</v-btn>


the {{ button.icon }} works. but the fn doesn't



Uncaught TypeError: button.fn is not a function
at !click (CardBase.vue?ac7a:32)
at invoker (vue.esm.js?efeb:2027)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826)


i also tried with this[button.fn], but that doesn't work either.



the solution is probably super easy, but i don't see it right now. how can i pass the method NAME? (like for minimize, i want to have methods defined once in that card component and just have to pass the name to reuse it.)



thanks :)










share|improve this question




















  • 1





    As you have it now, button.fn is defined as a string. If minimizeDashboardCard is a method in the parent scope, just pass the function. fn: minimizeDashboardCard

    – thanksd
    Nov 26 '18 at 21:48











  • but minimizeDashboardCardis only defined in the child. that's why i want to pass strings and not define the "minimize" method in every place i use it around the app. (edited the original question to make it clear the method is only defined in the child method)

    – devman
    Nov 26 '18 at 21:50




















0















I want to create components in a loop, passing properties to the child by name.



so:
<card-base v-for="(card, i) in cards" :key="i" :heading="card.heading" :width="card.width" class="dashboard-card" :cardHeaderButtons="[{icon: 'minimize', fn: 'minimizeDashboardCard'}]">



in my (child!) component i have defined the minimizeDashboardCard method,
and



                <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="button.fn"
>
<v-icon>
{{ button.icon }}
</v-icon>
</v-btn>


the {{ button.icon }} works. but the fn doesn't



Uncaught TypeError: button.fn is not a function
at !click (CardBase.vue?ac7a:32)
at invoker (vue.esm.js?efeb:2027)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826)


i also tried with this[button.fn], but that doesn't work either.



the solution is probably super easy, but i don't see it right now. how can i pass the method NAME? (like for minimize, i want to have methods defined once in that card component and just have to pass the name to reuse it.)



thanks :)










share|improve this question




















  • 1





    As you have it now, button.fn is defined as a string. If minimizeDashboardCard is a method in the parent scope, just pass the function. fn: minimizeDashboardCard

    – thanksd
    Nov 26 '18 at 21:48











  • but minimizeDashboardCardis only defined in the child. that's why i want to pass strings and not define the "minimize" method in every place i use it around the app. (edited the original question to make it clear the method is only defined in the child method)

    – devman
    Nov 26 '18 at 21:50
















0












0








0








I want to create components in a loop, passing properties to the child by name.



so:
<card-base v-for="(card, i) in cards" :key="i" :heading="card.heading" :width="card.width" class="dashboard-card" :cardHeaderButtons="[{icon: 'minimize', fn: 'minimizeDashboardCard'}]">



in my (child!) component i have defined the minimizeDashboardCard method,
and



                <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="button.fn"
>
<v-icon>
{{ button.icon }}
</v-icon>
</v-btn>


the {{ button.icon }} works. but the fn doesn't



Uncaught TypeError: button.fn is not a function
at !click (CardBase.vue?ac7a:32)
at invoker (vue.esm.js?efeb:2027)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826)


i also tried with this[button.fn], but that doesn't work either.



the solution is probably super easy, but i don't see it right now. how can i pass the method NAME? (like for minimize, i want to have methods defined once in that card component and just have to pass the name to reuse it.)



thanks :)










share|improve this question
















I want to create components in a loop, passing properties to the child by name.



so:
<card-base v-for="(card, i) in cards" :key="i" :heading="card.heading" :width="card.width" class="dashboard-card" :cardHeaderButtons="[{icon: 'minimize', fn: 'minimizeDashboardCard'}]">



in my (child!) component i have defined the minimizeDashboardCard method,
and



                <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="button.fn"
>
<v-icon>
{{ button.icon }}
</v-icon>
</v-btn>


the {{ button.icon }} works. but the fn doesn't



Uncaught TypeError: button.fn is not a function
at !click (CardBase.vue?ac7a:32)
at invoker (vue.esm.js?efeb:2027)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826)


i also tried with this[button.fn], but that doesn't work either.



the solution is probably super easy, but i don't see it right now. how can i pass the method NAME? (like for minimize, i want to have methods defined once in that card component and just have to pass the name to reuse it.)



thanks :)







javascript vue.js vuejs2 vue-component






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 21:53







devman

















asked Nov 26 '18 at 21:40









devmandevman

135113




135113








  • 1





    As you have it now, button.fn is defined as a string. If minimizeDashboardCard is a method in the parent scope, just pass the function. fn: minimizeDashboardCard

    – thanksd
    Nov 26 '18 at 21:48











  • but minimizeDashboardCardis only defined in the child. that's why i want to pass strings and not define the "minimize" method in every place i use it around the app. (edited the original question to make it clear the method is only defined in the child method)

    – devman
    Nov 26 '18 at 21:50
















  • 1





    As you have it now, button.fn is defined as a string. If minimizeDashboardCard is a method in the parent scope, just pass the function. fn: minimizeDashboardCard

    – thanksd
    Nov 26 '18 at 21:48











  • but minimizeDashboardCardis only defined in the child. that's why i want to pass strings and not define the "minimize" method in every place i use it around the app. (edited the original question to make it clear the method is only defined in the child method)

    – devman
    Nov 26 '18 at 21:50










1




1





As you have it now, button.fn is defined as a string. If minimizeDashboardCard is a method in the parent scope, just pass the function. fn: minimizeDashboardCard

– thanksd
Nov 26 '18 at 21:48





As you have it now, button.fn is defined as a string. If minimizeDashboardCard is a method in the parent scope, just pass the function. fn: minimizeDashboardCard

– thanksd
Nov 26 '18 at 21:48













but minimizeDashboardCardis only defined in the child. that's why i want to pass strings and not define the "minimize" method in every place i use it around the app. (edited the original question to make it clear the method is only defined in the child method)

– devman
Nov 26 '18 at 21:50







but minimizeDashboardCardis only defined in the child. that's why i want to pass strings and not define the "minimize" method in every place i use it around the app. (edited the original question to make it clear the method is only defined in the child method)

– devman
Nov 26 '18 at 21:50














3 Answers
3






active

oldest

votes


















2














How about using a helper method ..



methods: {
...
call(methodName) {
this[methodName]()
}
}


Then you can do this in your template ..





 <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="call(button.fn)"
>





share|improve this answer
























  • this worked, thanks!

    – devman
    Nov 26 '18 at 22:32



















1














maybe try @click.capture.stop="eval(button.fn).call()"



https://www.w3schools.com/js/js_function_call.asp






share|improve this answer


























  • unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

    – devman
    Nov 26 '18 at 22:08













  • updated. worked for me

    – Efrat
    Nov 26 '18 at 22:16













  • works now, but i think it's cleaner with a helper function. but thanks for your help :)

    – devman
    Nov 26 '18 at 22:36



















1














pass button as parameter to a method like :



   @click.capture.stop="callBtn(button)"


in your methods :



  methods:{
callBtn(button){
button.fn();
}
...
}





share|improve this answer
























  • didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

    – devman
    Nov 26 '18 at 22:33












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489498%2fvue-passing-onclick-method-name-via-props%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














How about using a helper method ..



methods: {
...
call(methodName) {
this[methodName]()
}
}


Then you can do this in your template ..





 <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="call(button.fn)"
>





share|improve this answer
























  • this worked, thanks!

    – devman
    Nov 26 '18 at 22:32
















2














How about using a helper method ..



methods: {
...
call(methodName) {
this[methodName]()
}
}


Then you can do this in your template ..





 <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="call(button.fn)"
>





share|improve this answer
























  • this worked, thanks!

    – devman
    Nov 26 '18 at 22:32














2












2








2







How about using a helper method ..



methods: {
...
call(methodName) {
this[methodName]()
}
}


Then you can do this in your template ..





 <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="call(button.fn)"
>





share|improve this answer













How about using a helper method ..



methods: {
...
call(methodName) {
this[methodName]()
}
}


Then you can do this in your template ..





 <v-btn icon flat class="header-button" v-for="(button, i) in cardHeaderButtons"
:key="i"
v-if="$vuetify.breakpoint.lgAndUp"
color="white"
@click.capture.stop="call(button.fn)"
>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 22:26









Husam IbrahimHusam Ibrahim

3,363518




3,363518













  • this worked, thanks!

    – devman
    Nov 26 '18 at 22:32



















  • this worked, thanks!

    – devman
    Nov 26 '18 at 22:32

















this worked, thanks!

– devman
Nov 26 '18 at 22:32





this worked, thanks!

– devman
Nov 26 '18 at 22:32













1














maybe try @click.capture.stop="eval(button.fn).call()"



https://www.w3schools.com/js/js_function_call.asp






share|improve this answer


























  • unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

    – devman
    Nov 26 '18 at 22:08













  • updated. worked for me

    – Efrat
    Nov 26 '18 at 22:16













  • works now, but i think it's cleaner with a helper function. but thanks for your help :)

    – devman
    Nov 26 '18 at 22:36
















1














maybe try @click.capture.stop="eval(button.fn).call()"



https://www.w3schools.com/js/js_function_call.asp






share|improve this answer


























  • unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

    – devman
    Nov 26 '18 at 22:08













  • updated. worked for me

    – Efrat
    Nov 26 '18 at 22:16













  • works now, but i think it's cleaner with a helper function. but thanks for your help :)

    – devman
    Nov 26 '18 at 22:36














1












1








1







maybe try @click.capture.stop="eval(button.fn).call()"



https://www.w3schools.com/js/js_function_call.asp






share|improve this answer















maybe try @click.capture.stop="eval(button.fn).call()"



https://www.w3schools.com/js/js_function_call.asp







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 22:18

























answered Nov 26 '18 at 22:05









EfratEfrat

1,012313




1,012313













  • unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

    – devman
    Nov 26 '18 at 22:08













  • updated. worked for me

    – Efrat
    Nov 26 '18 at 22:16













  • works now, but i think it's cleaner with a helper function. but thanks for your help :)

    – devman
    Nov 26 '18 at 22:36



















  • unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

    – devman
    Nov 26 '18 at 22:08













  • updated. worked for me

    – Efrat
    Nov 26 '18 at 22:16













  • works now, but i think it's cleaner with a helper function. but thanks for your help :)

    – devman
    Nov 26 '18 at 22:36

















unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

– devman
Nov 26 '18 at 22:08







unfortunately not :/ Uncaught TypeError: button.fn.call is not a function at !click (CardBase.vue?ac7a:32) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826) (both with and without parens)

– devman
Nov 26 '18 at 22:08















updated. worked for me

– Efrat
Nov 26 '18 at 22:16







updated. worked for me

– Efrat
Nov 26 '18 at 22:16















works now, but i think it's cleaner with a helper function. but thanks for your help :)

– devman
Nov 26 '18 at 22:36





works now, but i think it's cleaner with a helper function. but thanks for your help :)

– devman
Nov 26 '18 at 22:36











1














pass button as parameter to a method like :



   @click.capture.stop="callBtn(button)"


in your methods :



  methods:{
callBtn(button){
button.fn();
}
...
}





share|improve this answer
























  • didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

    – devman
    Nov 26 '18 at 22:33
















1














pass button as parameter to a method like :



   @click.capture.stop="callBtn(button)"


in your methods :



  methods:{
callBtn(button){
button.fn();
}
...
}





share|improve this answer
























  • didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

    – devman
    Nov 26 '18 at 22:33














1












1








1







pass button as parameter to a method like :



   @click.capture.stop="callBtn(button)"


in your methods :



  methods:{
callBtn(button){
button.fn();
}
...
}





share|improve this answer













pass button as parameter to a method like :



   @click.capture.stop="callBtn(button)"


in your methods :



  methods:{
callBtn(button){
button.fn();
}
...
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 22:19









Boussadjra BrahimBoussadjra Brahim

8,3153833




8,3153833













  • didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

    – devman
    Nov 26 '18 at 22:33



















  • didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

    – devman
    Nov 26 '18 at 22:33

















didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

– devman
Nov 26 '18 at 22:33





didn't work, it would still try to "call" fn (which is a string). in connection with Husam's answer, it worked now though. thanks to all of you!

– devman
Nov 26 '18 at 22:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489498%2fvue-passing-onclick-method-name-via-props%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen