<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Plabon Islam on Medium]]></title>
        <description><![CDATA[Stories by Plabon Islam on Medium]]></description>
        <link>https://medium.com/@plabonislam?source=rss-c85e88b41678------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PDeDXqNBwISfE_1q42PWtQ.jpeg</url>
            <title>Stories by Plabon Islam on Medium</title>
            <link>https://medium.com/@plabonislam?source=rss-c85e88b41678------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 12:06:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@plabonislam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Vue 3: Accessing Child Components Data From Parent Components ?]]></title>
            <link>https://blog.stackademic.com/vue-3-accessing-child-components-data-from-parent-components-65e6a41f768d?source=rss-c85e88b41678------2</link>
            <guid isPermaLink="false">https://medium.com/p/65e6a41f768d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vue-composition-api]]></category>
            <category><![CDATA[vue-component]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[vue-3]]></category>
            <dc:creator><![CDATA[Plabon Islam]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 05:03:02 GMT</pubDate>
            <atom:updated>2024-11-13T01:35:05.330Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vue 3: Communicating Between Parent and Child Components in Vue Using the Composition API.</h3><blockquote>I will explain the differences in data sharing between options and composition apis.Also, demonstrate potential ways to use the composition API to share data from a child component to parent component, such as emit and expose.</blockquote><p>If you create a child component using <strong>composition API,</strong> properties and methods of this component will be private.You can’t access private properties and methods from outside of that component.</p><pre>&lt;script setup&gt;<br>//composition api with setup script<br>// any property or methods are private <br>import { ref } from &#39;vue&#39;<br>const name= ref(&quot;child component&quot;);<br>const increase =()=&gt;{<br>  return count.value = count.value + 1;<br>} <br>&lt;/script&gt;</pre><p>But,if you create a component using <strong>options API</strong>, this child components properties and methods will be public. So, parent component can access this child components properties and methods using that component instance or $parent chains.</p><pre>&lt;script&gt;<br>// any property or method are public<br>//accessible from parent <br>export default {<br>    data() {<br>        return {<br>            name: &#39;&#39;,<br>        }<br>    },<br>    methods: {<br>         increase() {<br>         return this.count = this.count + 1;<br>        },   <br>    },<br>    <br>}<br>&lt;/script&gt;</pre><p><a href="https://differ.blog/differ-ai">Introducing DifferAI</a></p><p><strong>How can these two components communicate ?</strong></p><ol><li>parent-to-child interaction.</li><li>child-to-parent communication.</li><li>Global state management Such as Pinia or Vuex.</li></ol><p>Here, we will only talk about parent to child interaction and child to parent communication.</p><p><strong>Parent-to-child interaction:</strong></p><p>This interaction might happen for passing data from Parent to child or calling child components methods from parent components.</p><p>let’s discuss first , how we can pass data from parent components to child components.The most common ways to pass data from parent to child components are</p><ol><li>Props passing</li><li>Dependency Injection</li></ol><p><strong>Props Passing: </strong>It is a one way data binding and tightly coupled with parent to child components.</p><pre><br>*/child component*/<br>&lt;script setup&gt;<br> const props = defineProps({<br>  title: String,<br>  name: String<br>})<br>&lt;/script&gt;<br><br>&lt;template&gt;<br> &lt;h1&gt; {{title}}&lt;/h1&gt;<br> &lt;h1&gt; {{name}}&lt;/h1&gt;<br>&lt;/template&gt;<br></pre><p>now you can pass title and name property from parent components.</p><pre>*/Parent component*/<br>&lt;script setup&gt;<br>import Child from &quot;./child&quot;<br>import { ref } from &#39;vue&#39;<br>const title = ref(&quot;hey&quot;);<br><br>&lt;/script&gt;<br><br>&lt;template&gt;<br> &lt;Child :title=&quot;title&quot; name=&quot;john&quot; /&gt;<br>&lt;/template&gt;</pre><p>So if you have components architecture such as A-&gt;B-&gt;C-&gt;D. Now you want to pass a props from A -&gt; D. How can you achieve it.</p><p>As Props is tightly coupled with parent to child components. So, You have to pass props in every components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*XD6h-Uz4Kds5ISx9o9hlhw.png" /></figure><p><strong>Dependency Injection:</strong></p><p>This approach avoids tight parent-child coupling by allowing ancestor components to provide dependencies that child components can receive at runtime.In Vue, Dependency Injection pattern is achieved using provide and inject API.</p><p><strong>Provide: </strong>Make data available for descendant components.here, we are passing reactive value <em>title from parent components.</em></p><pre>*/Parent component*/<br>&lt;script setup&gt;<br>  import Child from &quot;./child&quot;<br>  import { ref ,provide} from &#39;vue&#39;<br>  const title = ref(&quot;hey&quot;);<br>  provide(&#39;title&#39;, title);<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>   &lt;Child /&gt;<br>&lt;/template&gt;</pre><p><strong>Inject:</strong>Injects data provided by an ancestor component.Here,Child components inject title data provided by parent components.</p><pre>*/child component*/<br>&lt;script setup&gt;<br>  import { ref ,inject} from &#39;vue&#39;<br>  const title = inject(&#39;title&#39;)<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;h1&gt; {{title}}&lt;/h1&gt;<br>&lt;/template&gt;</pre><p>So if you have components architecture such as A-&gt;B-&gt;C-&gt;D. Now you want to pass data from A -&gt; D. How can you achieve it.</p><p>As, Component D is a descendant of component A. So,you can provide data from component A and receive it in component D.So, in this approach Component B &amp; C doesn’t need to inject that value.</p><p>Now discuss about accessing Child Components Property and trigger method from Parent Components using combination of <strong>defineExpose</strong> and Te<strong>mplate ref</strong> .</p><p><strong>DefineExpose</strong> and <strong>Template ref</strong> :</p><p><strong>defineExpose: </strong>Components the Composition API are private. defineExpose method allows these components to expose their methods and properties to be publically accessible for the parent component.If you are using <strong>options</strong> API, in that case you don’t need to do this.</p><p><strong>Template Ref: </strong>Template ref allows parent components to trigger child components methods and access it’s property.let’s discuss it in details.</p><p><strong>Step 1</strong>:use the<strong> defineExpose</strong> method<strong> in your child component </strong>to expose properties and methods.</p><pre>defineExpose()</pre><p>I’ve made the name, isAdult, and count properties public, along with increase and decrease methods. Except for age property, the rest of them are publically accessible from the parent component.</p><pre>//ChildComponent.vue<br><br>&lt;script setup&gt;<br>import { ref, defineExpose } from &quot;vue&quot;;<br><br>const name = ref(&quot;child component&quot;);<br>const count = ref(1);<br>const isAdult = ref(false);<br>const age = ref(17);<br><br><br>const increase = () =&gt; {<br>  count.value = count.value + 1;<br>  isAdult.value = count.value &gt; age.value;<br>};<br><br>const decrease = () =&gt; {<br>  if (count.value &gt; 0) count.value = count.value - 1;<br>  isAdult.value = count.value &gt; age.value;<br>};<br><br>//age is private<br>//rest of the properties &amp; methods are public<br>defineExpose({<br>  name,<br>  count,<br>  isAdult,<br>  increase,<br>  decrease,<br>});<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;h5&gt;Child To Parent Data passing&lt;/h5&gt;<br>&lt;/template&gt;</pre><p><strong>Step 2</strong>: Now, In your parent component,you need to create an instance of the child component. This allows you to call methods defined in the child component from the parent.</p><p>Using <a href="https://medium.com/@plabonislam/how-to-access-dom-element-using-vue-3-compostion-api-2181d69abee6"><strong>Template Ref</strong></a><strong> ,</strong>you can <strong>create an instance of the child component.</strong> I have explained <a href="https://medium.com/@plabonislam/how-to-access-dom-element-using-vue-3-compostion-api-2181d69abee6"><strong>Template Ref</strong></a><strong> </strong>in my previous post. <a href="https://medium.com/@plabonislam/how-to-access-dom-element-using-vue-3-compostion-api-2181d69abee6">Click to go there</a>.</p><p>Here, I have added ChildComponent.vue in the parent component. I have also created a<strong>n instance named child</strong> from ChildComponent.vue. Using that instance, I have accessed publically available name,isAdult, and count properties, along with that instance i can trigger increase and decrease methods.</p><pre>//ParentComponent.vue<br><br>&lt;script setup&gt;<br>import ChildComponent from &quot;./ChildComponent.vue&quot;;<br>import { ref, onMounted, computed } from &quot;vue&quot;;<br><br>const child = ref(null);<br>const childComponetName = ref(&quot;&quot;);<br><br>const onChildIncrement = () =&gt; {<br>  child.value.increase();<br>  //increase child count<br>};<br><br>const onChildDecrement = () =&gt; {<br>  child.value.decrease();<br>  //decrease child count<br>};<br><br>onMounted(() =&gt; {<br>  childComponetName.value = child.value.name;<br>  // child components name is not updating<br>  //so i have read it after initial render<br>});<br><br>const childComponetCount = computed(() =&gt; {<br>  return child.value?.count;<br>  // if child count update it will return the latest count;<br>});<br>const isParentsChildAdult = computed(() =&gt; {<br>  return child.value?.isAdult;<br>  // if child isAdult update it will return the latest isAdult;<br>});<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;ChildComponent ref=&quot;child&quot; /&gt;<br>    <br>    &lt;p&gt;Count:{{ childComponetCount }}&lt;/p&gt;<br>    &lt;p&gt;Adult child? {{ isParentsChildAdult }}&lt;/p&gt;<br><br>    &lt;div class=&quot;btn&quot;&gt;<br>      &lt;button type=&quot;button&quot; @click=&quot;onChildDecrement&quot;&gt;decrement&lt;/button&gt;<br>      &lt;button type=&quot;button&quot; @click=&quot;onChildIncrement&quot;&gt;increment&lt;/button&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;style scoped&gt;<br>.btn {<br>  display: flex;<br>  justify-content: center;<br>  gap: 20px;<br>}<br>&lt;/style&gt;</pre><blockquote><em>👉 </em><strong>Also learn how to use Pinia for state management in Vue.js:</strong></blockquote><p><a href="https://differ.blog/p/a-comprehensive-guide-to-vue-js-state-management-using-pinia-207096">A Comprehensive Guide to Vue.js State Management using Pinia</a></p><p><strong>child-to-parent communication</strong></p><p><strong>Emit Event: U</strong>sing<strong> the e</strong>mit method, you can <strong>create a custom even</strong>t and <strong>pass data</strong> from the child to the parent component/listening component. Whenever <strong>a child component triggers an event </strong>, listening/parent component will listen to that event and act accordingly.</p><p>Emit methods accept a <strong>unique event name</strong> and event data. You can ignore event data, But the event name is mandatory.</p><pre>emit(&#39;customEventName&#39;, eventData);<br>         or<br>emit(&#39;customEventName&#39;);<br>//customEventName must be unique</pre><p>To achieve data passing between child component to parent component using composition API with emit , you need to follow <strong>two</strong> steps.</p><p><strong>Step 1</strong>:<strong> </strong>Create a custom event with emit in your child component<strong>.</strong> You can create custom events from your component <strong>template </strong>or <strong>script section</strong>.</p><p><strong>Script Section</strong>:If you use emit with script setup, you must declare the custom events directly in defineEmits.Here, I have declared both <strong>handleIncrease</strong> &amp; <strong>handleDecrease</strong> events in defineEmits. Later, I created those events and passed properties as event data.</p><pre><br>&lt;script setup&gt;<br>import { defineEmits, ref } from &quot;vue&quot;;<br><br>//list of declared custom events<br>const emit = defineEmits([&quot;handleIncrease&quot;, &quot;handleDecrease&quot;]);<br><br>const count = ref(1);<br>const age = ref(17);<br><br><br>const increase = () =&gt; {<br>  count.value = count.value + 1;<br>  const isAdult = count.value &gt; age.value;<br>  emit(&quot;handleIncrease&quot;, count.value, isAdult);<br> //custom event with event data<br>};<br>//<br>const decrease = () =&gt; {<br>  if (count.value &gt; 0) count.value = count.value - 1;<br>  const isAdult = count.value &gt; age.value;<br>  emit(&quot;handleDecrease&quot;, count.value, isAdult);<br>//custom event with event data<br>};<br>&lt;/script&gt;</pre><p><strong>Template Section:</strong>For Inline emit, you don’t need to declare any custom events. That’s why I have created a directly <strong>inline</strong> <strong>getSearchInput </strong>event from templates and passed search value as event data.</p><pre><br>&lt;script setup&gt;<br>import {ref } from &quot;vue&quot;;<br>const search = ref(&quot;&quot;);<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;input<br>      type=&quot;text&quot;<br>      class=&quot;form-control&quot;<br>      v-model=&quot;search&quot;<br>      @input=&quot;$emit(&#39;getSearchInput&#39;, search)&quot; /&gt;<br>  <br>&lt;/template&gt;<br><br>&lt;style scoped&gt;<br>.form-control {<br>  margin: 10px;<br>}<br><br>&lt;/style&gt;</pre><p><strong>Considering Both Cases Complete ChildComponent Code Using Emit :</strong></p><pre><br>//ChildComponent.vue<br>&lt;script setup&gt;<br>import { defineEmits, ref } from &quot;vue&quot;;<br><br>//list of declared custom events<br>const emit = defineEmits([&quot;handleIncrease&quot;, &quot;handleDecrease&quot;]);<br><br>const count = ref(1);<br>const age = ref(17);<br>const search = ref(&quot;&quot;);<br><br>const increase = () =&gt; {<br>  count.value = count.value + 1;<br>  const isAdult = count.value &gt; age.value;<br>  emit(&quot;handleIncrease&quot;, count.value, isAdult);<br>  //custom event with event data<br>};<br>//<br>const decrease = () =&gt; {<br>  if (count.value &gt; 0) count.value = count.value - 1;<br>  const isAdult = count.value &gt; age.value;<br>  emit(&quot;handleDecrease&quot;, count.value, isAdult);<br>  //custom event with event data<br>};<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;div class=&quot;btn&quot;&gt;<br>    &lt;button type=&quot;button&quot; @click=&quot;decrease&quot;&gt;decrement&lt;/button&gt;<br>    &lt;button type=&quot;button&quot; @click=&quot;increase&quot;&gt;increment&lt;/button&gt;<br>  &lt;/div&gt;<br><br>  &lt;!-- Inline emit getSearchInput &amp; searchValue is passing as argument--&gt;<br>  &lt;input<br>    type=&quot;text&quot;<br>    class=&quot;form-control&quot;<br>    v-model=&quot;search&quot;<br>    @input=&quot;$emit(&#39;getSearchInput&#39;,search)&quot;<br>  /&gt;<br>&lt;/template&gt;<br><br>&lt;style scoped&gt;<br>.form-control {<br>  margin: 10px;<br>}<br>.btn {<br>  display: flex;<br>  justify-content: center;<br>  gap: 20px;<br>}<br>&lt;/style&gt;</pre><p><strong>Step 2</strong>:In your parent /listening component, you need to add a child component. Now you have to bind those custom events in Vue.js templates using the <strong>on directive or its shorthand @ symbol.</strong></p><p>So, I have added ChildComponent.vue in ParentComponent.vue and binding all three custom events .</p><pre>//ParentComponent.vue<br>&lt;script setup&gt;<br><br>import ChildComponent from &quot;./ChildComponent.vue&quot;;<br>import { ref } from &quot;vue&quot;;<br>const parentCount = ref(1);<br>const isParentsChildAdult = ref(false);<br><br>const increaseParent = (childCount, isAdult) =&gt; {<br>  parentCount.value = childCount;<br>  isParentsChildAdult.value = isAdult;<br> //Whenever in child component user click increase method,<br> //handleIncrease event will listen and act accordingly<br>};<br><br>const decreaseParent = (childCount, isAdult) =&gt; {<br>  parentCount.value = childCount;<br>  isParentsChildAdult.value = isAdult;<br> //Whenever in child component user click descrease method,<br> //handleDecrease event will listen and act accordingly<br><br>};<br><br>const showAlert = (value) =&gt; {<br>  alert(value);<br> //Whenever in child component user fills input ,<br> //getSearchInput event  will listen <br> <br>};<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;p&gt;{{ parentCount }}&lt;/p&gt;<br>  &lt;p&gt;Adult child? {{ isParentsChildAdult }}&lt;/p&gt;<br>  &lt;ChildComponent<br>    @handleIncrease=&quot;increaseParent&quot;<br>    @handleDecrease=&quot;decreaseParent&quot;<br>    @getSearchInput=&quot;showAlert&quot; /&gt;<br>&lt;/template&gt;<br><br><br></pre><blockquote><strong>Conclusion</strong> 🎓</blockquote><ul><li><strong>emit</strong> is One way event driven data flow from child component to parent components! 👏</li><li><strong>Expose and template Ref</strong> allow controlled access of child components property and methods from parent components.</li><li>Props and Dependency Injection allows to pass data from Parent to child Components</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65e6a41f768d" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/vue-3-accessing-child-components-data-from-parent-components-65e6a41f768d">Vue 3: Accessing Child Components Data From Parent Components ?</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Access DOM Element Using Vue 3 & Composition API]]></title>
            <link>https://blog.stackademic.com/how-to-access-dom-element-using-vue-3-compostion-api-2181d69abee6?source=rss-c85e88b41678------2</link>
            <guid isPermaLink="false">https://medium.com/p/2181d69abee6</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[vue-3]]></category>
            <dc:creator><![CDATA[Plabon Islam]]></dc:creator>
            <pubDate>Thu, 10 Aug 2023 21:13:57 GMT</pubDate>
            <atom:updated>2024-09-24T12:19:02.890Z</atom:updated>
            <content:encoded><![CDATA[<p>In javascript, we can easily target a dom using getElementById, getElementByClassName, getElementByTagname, or querySelector. But dom manipulation is costly. It’s not a vue way to target a dom element. So, how can we achieve this in a vue way?</p><p><strong><em>Template Ref: </em></strong><em>In vue,</em><strong><em> t</em></strong>emplate ref allows to target a dom elements or instance of child component after their initial rendering.</p><p>Suppose, you want to target <strong>h1</strong> elemenet from your component .</p><pre>&lt;template&gt;<br> &lt;h1&gt; hello world &lt;/h1&gt;<br>&lt;/template&gt;</pre><p>To achive this with composition api you need to follow <strong>three</strong> steps:</p><p><strong>step 1: </strong>Add <strong>ref</strong> attribute with your target element.</p><pre>&lt;template&gt;<br>  &lt;h1 ref=&quot;headline&quot;&gt; hello world &lt;/h1&gt;<br>&lt;/template&gt;</pre><p><strong>step 2</strong>: Declare a <strong>reactive state</strong> for that element with the <strong>same template ref name.</strong>It will hold the reference of the element.</p><pre>&lt;script setup&gt;<br>import { ref } from &quot;vue&quot;;<br>const headline=ref(null);<br>&lt;/script&gt;</pre><p><strong>step 3</strong>: In Vue 3, the script setup runs before anything.So,you can obtain the element instance in that reactive state when the component will render.</p><p><a href="https://differ.blog/differ-ai">Introducing DifferAI</a></p><p>As,onBeforeMount hook runs before dom creation.So, reactive state will be null.But onMounted/onUpdated hook is called after the dom creation. So you can obtain the element instance here.</p><pre>&lt;script setup&gt;<br> import { onBeforeMount,onMounted, onUpdated, ref } from &quot;vue&quot;;<br> const headline=ref(null);<br><br> onBeforeMount(()=&gt;{<br> console.log(headline.value);<br> //this hook runs before component is rendered &amp;&amp; print null<br><br> }<br> onMounted(()=&gt;{<br> console.log(headline.value)<br>//print the element<br> }<br><br>onUpdated(()=&gt;{<br> console.log(headline.value)<br>//print the element<br> }<br>&lt;/script&gt;</pre><h3>Complete Code</h3><pre>&lt;script setup&gt;<br> import { onMounted, ref } from &quot;vue&quot;;<br> const headline=ref(null);<br> onMounted(()=&gt;{<br> console.log(headline.value) <br> }<br>&lt;/script&gt;<br><br>&lt;template&gt;<br> &lt;h1 ref=&quot;headline&quot;&gt; hello world &lt;/h1&gt;<br>&lt;/template&gt;</pre><blockquote><em>👉 </em><strong>Also learn how to use Pinia for Vue.js state management:</strong></blockquote><p><a href="https://differ.blog/p/a-comprehensive-guide-to-vue-js-state-management-using-pinia-207096">A Comprehensive Guide to Vue.js State Management using Pinia</a></p><h3>Stackademic 🎓</h3><p>Thank you for reading until the end. Before you go:</p><ul><li>Please consider <strong>clapping</strong> and <strong>following</strong> the writer! 👏</li><li>Follow us <a href="https://twitter.com/stackademichq"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/stackademic"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/c/stackademic"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a></li><li>Visit our other platforms: <a href="https://plainenglish.io/"><strong>In Plain English</strong></a> | <a href="https://cofeed.app/"><strong>CoFeed</strong></a> | <a href="https://differ.blog/"><strong>Differ</strong></a></li><li>More content at <a href="https://stackademic.com/"><strong>Stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2181d69abee6" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/how-to-access-dom-element-using-vue-3-compostion-api-2181d69abee6">How to Access DOM Element Using Vue 3 &amp; Composition API</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>