{"id":24986,"date":"2022-12-07T03:35:12","date_gmt":"2022-12-06T22:05:12","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/"},"modified":"2022-12-07T03:35:12","modified_gmt":"2022-12-06T22:05:12","slug":"solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/","title":{"rendered":"[Solved] How can I use data in the child component that are updated in parent component using Vue.js?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-52341141\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"52341141\" data-parentid=\"52333835\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>One solution is to use the <code>sync<\/code> modifier along with computed getters and setters:<\/p>\n<p><strong>Parent Component<\/strong><\/p>\n<pre><code>&lt;template&gt;\n    ...\n        &lt;PaymentMethod :method.sync=\"method\" :term.sync=\"term\"\/&gt;\n        ...\n        &lt;b-btn class=\"float-right\" variant=\"primary\" @click=\"add\"&gt;\n             OK\n        &lt;\/b-btn&gt;\n    ...\n&lt;\/template&gt;\n&lt;script&gt;\n    export default {\n        data () {\n            return {\n                method: null,\n                term: null\n            }\n        },\n        ...\n    }\n&lt;\/script&gt;\n<\/code><\/pre>\n<p><strong>Child Component<\/strong><\/p>\n<pre><code>&lt;template&gt;\n    ...\n        &lt;b-form-select v-model=\"_method\" :options=\"methodOptions\" \/&gt;\n        ...\n        &lt;b-form-select v-model=\"_term\" :options=\"termOptions\" \/&gt;\n    ...\n&lt;\/template&gt;\n&lt;script&gt;\n    export default {\n        props: ['method', 'term'],\n        computed: {\n            _method: {\n               get () {\n                   return this.method\n               },\n               set (value) {\n                   this.$emit('update:method', value)\n               }\n            },\n            _term: {\n               get () {\n                   return this.term\n               },\n               set (value) {\n                   this.$emit('update:term', value)\n               }\n            },\n        },\n        ...\n    }\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>Now with the parent component&#8217;s <code>add<\/code> method you have access to the child&#8217;s selected <code>method<\/code> and <code>term<\/code> options:<\/p>\n<pre><code>methods: {\n    add() {\n        \/\/ this.method is the value of _method \n        \/\/ this.term is the value of _term\n    }\n}\n<\/code><\/pre>\n<p><strong>Update<\/strong><\/p>\n<p>Since you&#8217;ve stated you want both the value and text of the selected term \/ method, I would suggest the following changes:<\/p>\n<p><strong>Parent<\/strong><\/p>\n<pre><code>&lt;template&gt;\n    ...\n        &lt;PaymentMethod :methods=\"methods\" \n                       :terms=\"terms\"  \n                       :method.sync=\"method\" \n                       :term.sync=\"term\"\/&gt;\n        ...\n        &lt;b-btn class=\"float-right\" variant=\"primary\" @click=\"add\"&gt;\n             OK\n        &lt;\/b-btn&gt;\n    ...\n&lt;\/template&gt;\n&lt;script&gt;\n    export default {\n        data () {\n            return {\n                \/\/ define your method and term arrays in the parent component.\n                \/\/ pass them as props to the child component.\n                methods: [{...}, {...}, {...}],\n                terms: [{...}, {...}, {...}],\n                method: null,\n                term: null\n            }\n        },\n        \/\/ use computed properties to retrieve the selected method \/ term option.\n        computed: {\n            selectedMethod () {\n                return this.methods.find(method =&gt; method.value === this.method)\n            },\n            selectedTerm () {\n                return this.terms.find(term =&gt; term.value === this.term)\n            },\n        }\n        ...\n    }\n&lt;\/script&gt;\n<\/code><\/pre>\n<p><strong>Child<\/strong><\/p>\n<pre><code>&lt;template&gt;\n    ...\n        &lt;b-form-select v-model=\"_method\" :options=\"methods\" \/&gt;\n        ...\n        &lt;b-form-select v-model=\"_term\" :options=\"terms\" \/&gt;\n    ...\n&lt;\/template&gt;\n&lt;script&gt;\n    export default {\n        props: ['method', 'term', 'methods', 'terms'],\n        computed: {\n            _method: {\n               get () {\n                   return this.method\n               },\n               set (value) {\n                   this.$emit('update:method', value)\n               }\n            },\n            _term: {\n               get () {\n                   return this.term\n               },\n               set (value) {\n                   this.$emit('update:term', value)\n               }\n            },\n        },\n        ...\n    }\n&lt;\/script&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How can I use data in the child component that are updated in parent component using Vue.js? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] One solution is to use the sync modifier along with computed getters and setters: Parent Component &lt;template&gt; &#8230; &lt;PaymentMethod :method.sync=&#8221;method&#8221; :term.sync=&#8221;term&#8221;\/&gt; &#8230; &lt;b-btn class=&#8221;float-right&#8221; variant=&#8221;primary&#8221; @click=&#8221;add&#8221;&gt; OK &lt;\/b-btn&gt; &#8230; &lt;\/template&gt; &lt;script&gt; export default { data () { return { method: null, term: null } }, &#8230; } &lt;\/script&gt; Child Component &lt;template&gt; &#8230; &lt;b-form-select v-model=&#8221;_method&#8221; &#8230; <a title=\"[Solved] How can I use data in the child component that are updated in parent component using Vue.js?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/\" aria-label=\"More on [Solved] How can I use data in the child component that are updated in parent component using Vue.js?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[5253,333,5252,808,5251],"class_list":["post-24986","post","type-post","status-publish","format-standard","hentry","category-solved","tag-bootstrap-vue","tag-javascript","tag-vue-component","tag-vue-js","tag-vuejs2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How can I use data in the child component that are updated in parent component using Vue.js? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How can I use data in the child component that are updated in parent component using Vue.js? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] One solution is to use the sync modifier along with computed getters and setters: Parent Component &lt;template&gt; ... &lt;PaymentMethod :method.sync=&quot;method&quot; :term.sync=&quot;term&quot;\/&gt; ... &lt;b-btn class=&quot;float-right&quot; variant=&quot;primary&quot; @click=&quot;add&quot;&gt; OK &lt;\/b-btn&gt; ... &lt;\/template&gt; &lt;script&gt; export default { data () { return { method: null, term: null } }, ... } &lt;\/script&gt; Child Component &lt;template&gt; ... &lt;b-form-select v-model=&quot;_method&quot; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-06T22:05:12+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How can I use data in the child component that are updated in parent component using Vue.js?\",\"datePublished\":\"2022-12-06T22:05:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/\"},\"wordCount\":99,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"bootstrap-vue\",\"javascript\",\"vue-component\",\"vue.js\",\"vuejs2\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/\",\"name\":\"[Solved] How can I use data in the child component that are updated in parent component using Vue.js? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-12-06T22:05:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How can I use data in the child component that are updated in parent component using Vue.js?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How can I use data in the child component that are updated in parent component using Vue.js? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How can I use data in the child component that are updated in parent component using Vue.js? - JassWeb","og_description":"[ad_1] One solution is to use the sync modifier along with computed getters and setters: Parent Component &lt;template&gt; ... &lt;PaymentMethod :method.sync=\"method\" :term.sync=\"term\"\/&gt; ... &lt;b-btn class=\"float-right\" variant=\"primary\" @click=\"add\"&gt; OK &lt;\/b-btn&gt; ... &lt;\/template&gt; &lt;script&gt; export default { data () { return { method: null, term: null } }, ... } &lt;\/script&gt; Child Component &lt;template&gt; ... &lt;b-form-select v-model=\"_method\" ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/","og_site_name":"JassWeb","article_published_time":"2022-12-06T22:05:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How can I use data in the child component that are updated in parent component using Vue.js?","datePublished":"2022-12-06T22:05:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/"},"wordCount":99,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["bootstrap-vue","javascript","vue-component","vue.js","vuejs2"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/","url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/","name":"[Solved] How can I use data in the child component that are updated in parent component using Vue.js? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-06T22:05:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-use-data-in-the-child-component-that-are-updated-in-parent-component-using-vue-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How can I use data in the child component that are updated in parent component using Vue.js?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=24986"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24986\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=24986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=24986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=24986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}