<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>vuejs3 Archives - Program Easily</title>
	<atom:link href="https://programeasily.com/tag/vuejs3/feed/" rel="self" type="application/rss+xml" />
	<link>https://programeasily.com/tag/vuejs3/</link>
	<description>Program Easily helps people to learn about software programs in a easy manner.</description>
	<lastBuildDate>Thu, 15 Dec 2022 03:57:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/cropped-logo.png?fit=32%2C32&#038;ssl=1</url>
	<title>vuejs3 Archives - Program Easily</title>
	<link>https://programeasily.com/tag/vuejs3/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">187075990</site>	<item>
		<title>Teleport in Vue 3 with Example</title>
		<link>https://programeasily.com/2021/07/13/teleport-in-vue-3-with-example/</link>
					<comments>https://programeasily.com/2021/07/13/teleport-in-vue-3-with-example/#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 06:47:54 +0000</pubDate>
				<category><![CDATA[Vuejs]]></category>
		<category><![CDATA[vuejs]]></category>
		<category><![CDATA[vuejs3]]></category>
		<guid isPermaLink="false">https://programeasily.com/?p=321</guid>

					<description><![CDATA[<p>Creating the web pages using components is a good idea. So that we can re-use the logic and structure of the design. We have to make a component hierarchy or tree to make the UI better. In Vue.js we are using template string or render function to build the HTML DOM in the components. The Vue compiler will compile it and make the div elements for us However, sometimes logically, we need one component which could be a child to...</p>
<p class="read-more"><a class="btn btn-default" href="https://programeasily.com/2021/07/13/teleport-in-vue-3-with-example/"> Read More<span class="screen-reader-text">  Read More</span></a></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2021/07/13/teleport-in-vue-3-with-example/">Teleport in Vue 3 with Example</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span style="font-weight: 400;">Creating the web pages using components is a good idea. So that we can re-use the logic and structure of the design. We have to make a component hierarchy or tree to make the UI better. In Vue.js we are using template string or render function to build the HTML DOM in the components. The Vue compiler will compile it and make the div elements for us</span></p>
<p><span style="font-weight: 400;">However, sometimes logically, we need one component which could be a child to its parent component, But the component HTML div should not be nest to its parent. Meanwhile we need to place it somewhere else in the DOM. Little bit confusing right.</span></p>
<p><span style="font-weight: 400;">Generally the HTML tree structure will be the same as component tree structure. But if you want to change one component div&#8217;s parent somewhere else in the DOM, we can go for teleport in Vue.js. It is an advanced feature introduced in Vue.js 3. If you are not familiar with Vue.js You can refer to our page</span><a href="https://programeasily.com/vue/"> <span style="font-weight: 400;">Vue</span></a><span style="font-weight: 400;"> Let&#8217;s go to the topic in detail.</span></p>
<p><img fetchpriority="high" decoding="async" class="aligncenter wp-image-466 size-large" src="https://i0.wp.com/programeasily.com/wp-content/uploads/2021/07/Teleport-in-Vue-3-with-Example-1.png?resize=640%2C535&#038;ssl=1" alt="Teleport in Vue 3 with Example" width="640" height="535" srcset="https://i0.wp.com/programeasily.com/wp-content/uploads/2021/07/Teleport-in-Vue-3-with-Example-1.png?resize=1024%2C856&amp;ssl=1 1024w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/07/Teleport-in-Vue-3-with-Example-1.png?resize=300%2C251&amp;ssl=1 300w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/07/Teleport-in-Vue-3-with-Example-1.png?resize=768%2C642&amp;ssl=1 768w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/07/Teleport-in-Vue-3-with-Example-1.png?resize=323%2C270&amp;ssl=1 323w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/07/Teleport-in-Vue-3-with-Example-1.png?w=1280&amp;ssl=1 1280w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /></p>
<h2>Teleport</h2>
<p><strong>Teleport will change the parent element of a component. Note that teleport will move actual DOM nodes instead of recreating it.</strong> This will maintain the same component instance. Teleport has two attributes.</p>
<ol>
<li>to &#8211; String</li>
<li>disabled &#8211; Boolean</li>
</ol>
<p><code>to</code> &#8211; <code>string</code> is <span style="font-weight: 400;">a prop denoting a target parent element where the teleport will move it&#8217;s content. It is a mandatory prop. This prop can be a valid query selector, or an HTML element (Example : body).</span></p>
<pre><code class="language-markup">&lt;teleport to="#to-parent-id" /&gt; 
&lt;teleport to=".to-parent-id" /&gt; 
&lt;teleport to="[to-parent-data-teleport]" /&gt;</code></pre>
<p><code>disabled</code> &#8211; <code>boolean</code> is a prop which disables the teleport operation. This is an optional Boolean prop. If we apply the disable prop, this will not move the teleport content to the target element. <strong>At the same time it will render the elements where you specified the teleport tag just like normal components.</strong> A great example for this use case is, streaming the video in either pop up or normally like below,</p>
<pre><code class="language-markup">&lt;teleport to="#popup" :disabled="popupOrInline"&gt;
  &lt;video src="./my-movie.mp4"&gt;
&lt;/teleport&gt;</code></pre>
<h2>Teleport in Vue 3 with Example</h2>
<p>Generally, we can achieve the same operation without teleport also. If we need to change the positions of the elements but quickly ,the process becomes difficult. But teleport makes the work simple for us. In our example, we are going to teleport a component to HTML  <code>#toPlace</code> div. Firstly we are creating Teleport component as follows</p>
<pre><code class="language-javascript">// TeleportComp.vue
&lt;template&gt;
  &lt;div&gt;
    &lt;teleport :to="teleportTo" :disabled="disableTeleport"&gt;
      &lt;TeleportChild:imagePath="imagePath" /&gt;
    &lt;/teleport&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import TeleportChild from "./TeleportChild.vue";
export default {
  components: {
    TeleportChild,
  },
  props: {
    teleportTo: {
      type: String,
      default: "body",
    },
    imagePath: String,
    disableTeleport: Boolean,
  },
};
&lt;/script&gt;
</code></pre>
<p>Secondly we are creating the TeleportChild.vue component as follows,</p>
<pre><code class="language-javascript">// TeleportChild.vue
&lt;template&gt;
  &lt;img
    :src="imagePath"
    alt="Teleport child Text"
    :style="{ width: '100%', height: '100%' }"
  /&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  props: {
    imagePath: String,
    altText: String,
  },
};
&lt;/script&gt;</code></pre>
<p>In the above example,  we are using the child component <code>TeleportChild.vue</code> inside the Teleport Component. Note that here, logically <code>TeleportChild.vue</code> component will remain as child for <code>TeleportComp.vue</code> component even when it&#8217;s position changed (But actually not). Moreover the Vue.js Dev-tools also will have the same component hierarchy structure. Now we are creating the <code>TeleportDemo.vue</code> file to check the behaviors. Here we are enabling teleport operation based on <code>disableTeleport</code> flag.</p>
<pre><code class="language-javascript">// TeleportDemo.vue
&lt;template&gt;
  &lt;div id="toPlace"&gt;&lt;/div&gt;
  &lt;div :style="{ width: '200px', height: '200px' }"&gt;
    &lt;button class="btn" @click="onClickBtn"&gt;
      {{ disableTeleport ? "Move to toPlace" : "Come Back" }}
    &lt;/button&gt;
    &lt;TeleportComp
      teleportTo="#toPlace"
      imagePath="logo_transparent.png"
      :disableTeleport="disableTeleport"
    /&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { ref } from "vue";
import TeleportComp from "../components/teleport/TeleportComp.vue";
export default {
  components: {
    TeleportComp,
  },
  setup() {
    let disableTeleport = ref(true);
    const onClickBtn = () =&gt; {
      disableTeleport.value = !disableTeleport.value;
    };
    return {
      onClickBtn,
      disableTeleport,
    };
  },
};
&lt;/script&gt;
&lt;style scoped&gt;
#toPlace {
  width: 200px;
  height: 200px;
  margin: auto;
  display: flex;
  justify-content: center;
}
.btn {
  background-color: #4999f5;
  border: none;
  color: white;
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
}
.btn:hover {
  background-color: #0417c0;
  color: white;
}
&lt;/style&gt;</code></pre>
<p>In the above example, if <code>disableTeleport</code> is set to true, then the teleport component acts as a normal component. if <code>disableTeleport</code> is set to false, the teleport component will be moved to <code>#toPlace</code> div element. Teleport will not change the hierarchy of component tree structure logically. But the effect will reflect in the HTML DOM. Now the image element moved to <code>#toPlace</code> element. You can check the full working example below,</p>
<p><iframe style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;" title="vue-3-basics-program-easily" src="https://codesandbox.io/embed/github/programeasily/vue-3-basics/tree/main/?fontsize=14&amp;hidenavigation=1&amp;initialpath=teleportdemo&amp;module=%2Fsrc%2Fcomponents%2Fteleport%2FTeleportComp.vue&amp;theme=dark" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe></p>
<h2>Multiple Teleport in Vue 3 with Example</h2>
<p>Now we are going to see multiple teleport operations. Here the same target element will have multiple teleport components. When we move the teleport components, It will follow the moving order and append to the target element.</p>
<pre><code class="language-javascript">&lt;teleport to="#toPlace"&gt;
  &lt;div&gt;A&lt;/div&gt;
&lt;/teleport&gt;
&lt;teleport to="#toPlace"&gt;
  &lt;div&gt;B&lt;/div&gt;
&lt;/teleport&gt;

&lt;!-- result--&gt;
&lt;div id="toPlace"&gt;
  &lt;div&gt;A&lt;/div&gt;
  &lt;div&gt;B&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>In the above example, the <code>#toPlace</code> element will have A,B div elements on teleport operation. Now we are creating <code>TeleportMultipleDemo.vue</code> file where we are moving two teleport components to same <code>#toPlace</code> target element. You can refer the full working example below</p>
<p><iframe style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;" title="vue-3-basics-program-easily" src="https://codesandbox.io/embed/github/programeasily/vue-3-basics/tree/main/?fontsize=14&amp;hidenavigation=1&amp;initialpath=teleportmultipledemo&amp;module=%2Fsrc%2Fpage%2FTeleportMultipleDemo.vue&amp;theme=dark" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe></p>
<h2>Summary</h2>
<ul>
<li><strong>Teleport will change the parent of a component. Note that teleport will move actual DOM nodes instead of recreating it.</strong></li>
<li style="text-align: left;">Even though teleport changes the parent elements, it will maintain the instances of components.</li>
<li>If disabled flag is given, it will render the elements where you specified the teleport tag just like normal components.</li>
<li>The same target element will have multiple teleport components.</li>
</ul>
<h2>Reference</h2>
<ul>
<li><a href="https://v3.vuejs.org/guide/teleport.html#teleport">https://v3.vuejs.org/guide/teleport.html#teleport</a></li>
<li><a href="https://v3.vuejs.org/api/built-in-components.html#teleport">https://v3.vuejs.org/api/built-in-components.html#teleport</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://programeasily.com/2021/07/13/teleport-in-vue-3-with-example/">Teleport in Vue 3 with Example</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://programeasily.com/2021/07/13/teleport-in-vue-3-with-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">321</post-id>	</item>
		<item>
		<title>Provide And Inject in Vue.js</title>
		<link>https://programeasily.com/2021/04/17/provide-and-inject-in-vue-js/</link>
					<comments>https://programeasily.com/2021/04/17/provide-and-inject-in-vue-js/#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Sat, 17 Apr 2021 14:17:55 +0000</pubDate>
				<category><![CDATA[Vuejs]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[vuejs]]></category>
		<category><![CDATA[vuejs3]]></category>
		<guid isPermaLink="false">http://programeasily.com/?p=37</guid>

					<description><![CDATA[<p>Provide and Inject are the advanced concepts of Vue.js Components module. It&#8217;s not mostly needed for small structured components. But imagine if the structures are deeply nested. Then we are in need to pass data from parent to innermost child component. It&#8217;s a completely difficult situation right now. To solve this problem, Vue.js introduced Provide and Inject concepts. Here the parent component provides the dependency to all its children irrespective of how long the component hierarchy is. As the name...</p>
<p class="read-more"><a class="btn btn-default" href="https://programeasily.com/2021/04/17/provide-and-inject-in-vue-js/"> Read More<span class="screen-reader-text">  Read More</span></a></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2021/04/17/provide-and-inject-in-vue-js/">Provide And Inject in Vue.js</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><em>Provide and Inject</em> are the advanced concepts of Vue.js Components module. It&#8217;s not mostly needed for small structured components. But imagine if the structures are deeply nested. Then we are in need to pass data from parent to innermost child component. It&#8217;s a completely difficult situation right now.</p>
<p>To solve this problem, Vue.js introduced <em>Provide and Inject</em> concepts. Here the parent component provides the dependency to all its children irrespective of how long the component hierarchy is. As the name suggests, the parent component has a <em>provide</em> option and the child component has an <em>inject</em> option. <strong>The provide option will <em>provide</em> the data and <em>inject</em> will acquire this data.</strong></p>
<p>Let&#8217;s take a deep dive into this with an example. Read <a href="https://programeasily.com/2020/12/22/components-in-vue3/" target="_blank" rel="noopener">Components in Vue3</a> and <a href="https://programeasily.com/2020/12/31/single-file-components-vue-3/" target="_blank" rel="noopener">Single File Components Vue 3</a> if you are new to Vue.js components.</p>
<h2><img decoding="async" class="aligncenter size-full wp-image-241" src="https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?resize=640%2C427&#038;ssl=1" alt="provideandinjectinvue.js" width="640" height="427" srcset="https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?w=1920&amp;ssl=1 1920w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?resize=300%2C200&amp;ssl=1 300w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?resize=1024%2C683&amp;ssl=1 1024w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?resize=768%2C512&amp;ssl=1 768w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?resize=1536%2C1024&amp;ssl=1 1536w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?resize=405%2C270&amp;ssl=1 405w, https://i0.wp.com/programeasily.com/wp-content/uploads/2021/04/provideandinjectinvue.js.jpg?w=1280&amp;ssl=1 1280w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /></h2>
<pre><code class="language-markup">Root
└─ Page (&lt;- Use Provide Here -&gt;)
   ├─ App Bar
   └─ Layout
      ├─ Drawer Menu
      └─ Main Floor
         └─ Header Room (&lt;- Use Inject Here -&gt;)
         └─ Content Room
   └─ Footer</code></pre>
<p>Let me take the above example, where we want Page component data in Header room component. We can pass as prop data from <strong>Page -&gt; Layout -&gt; Main Floor -&gt; Header Room</strong>  components one by one. Alternatively we can use <em>provide</em>/<em>inject</em> here.</p>
<h3>Use Provide</h3>
<p>Firstly, our goal is to pass Page Component data to it&#8217;s inner child Header Room Component without passing as prop data.  So we are using <em>Provide</em> in Page.vue.</p>
<pre><code class="language-javascript">Page.vue

&lt;template&gt;
  &lt;div /&gt;
  &lt;!-- &lt;AppBar /&gt; 
  // Here we supposed to pass pageProperties
  // to Layout and Layout -&gt; Main Floor -&gt; Header Room
  // But with the help of provide/inject we can directly
  // use this in Header Room component --&gt;
  &lt;Layout /&gt;
&lt;/template&gt;

&lt;script&gt;
import Layout from "./Layout";
export default {
  components: {
    Layout,
  },
  provide: {
    pageProperties: {
      pageName: "Provide/Inject",
      pageVersion: "1.0.0",
      pageUser: "admin",
    },
  },
};
&lt;/script&gt;</code></pre>
<h3>Use Inject</h3>
<p>Secondly, we will be using <em>inject</em> in HeaderRoom.vue Component to get the data given in Page.vue Component with <em>provide</em> option.</p>
<pre><code class="language-javascript">HeaderRoom.vue

&lt;template&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageName }}&lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageVersion }}&lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageUser }}&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  inject: ["pageProperties"],
};
&lt;/script&gt;</code></pre>
<h4>The full working example</h4>
<p><iframe style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;" title="vue-3-basics-program-easily" src="https://codesandbox.io/embed/github/programeasily/vue-3-basics/tree/main/?fontsize=14&amp;hidenavigation=1&amp;view=split&amp;initialpath=provideandinjectdemo&amp;module=%2Fsrc%2Fcomponents%2Fprovideinject%2FPage.vue&amp;theme=dark" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start">﻿</span></iframe></p>
<h3>Note :</h3>
<p>Some points to be noted here,</p>
<ol>
<li><span style="font-weight: 400;">Parent component wouldn&#8217;t know which child is going to use the provided data.</span></li>
<li><span style="font-weight: 400;">Likewise the child component wouldn&#8217;t be aware where the injected data coming from.</span></li>
</ol>
<h2>Provide And Inject in Vue.js with Composition API</h2>
<p>In addition, we can use <em>provide</em> and <em>inject</em> in the composition API also. We have to call both methods inside the <a href="https://programeasily.com/2021/01/04/setup-function-in-composition-api-vue-3/" target="_blank" rel="noopener">setup function.</a></p>
<h3>Provide in setup</h3>
<p>We have to import <em>provide</em> function from vue module. The <em>provide</em> function has two parameters,</p>
<ol>
<li>The Property Name (type should be string)</li>
<li>The Property Value  (type can be anything)</li>
</ol>
<p>we are updating  Page.vue component as follows,</p>
<pre><code class="language-javascript">Page.vue

&lt;template&gt;
  &lt;div /&gt;
  &lt;!-- &lt;AppBar /&gt; 
  // Here we supposed to pass pageProperties
  // to Layout and Layout -&gt; Main Floor -&gt; Header Room
  // But with the help of provide/inject we can directly
  // use this in Header Room component --&gt;
  &lt;Layout /&gt;
&lt;/template&gt;

&lt;script&gt;
import { provide } from "vue";
import Layout from "../provideinject/Layout";
export default {
  components: {
    Layout,
  },
  setup() {
    provide("pageProperties", {
      pageName: "Provide/Inject",
      pageVersion: "1.0.0",
      pageUser: "admin",
    });
  },
};
&lt;/script&gt;</code></pre>
<h3>Inject in setup</h3>
<p><span style="font-weight: 400;">We have to import the <em>inject</em> function from the vue module</span>. The <em>inject</em> function has two parameters,</p>
<ol>
<li>The property Name</li>
<li>Default value (optional)</li>
</ol>
<p>we are updating  HeaderRoom.vue component as follows,</p>
<pre><code class="language-javascript">HeaderRomm.vue

&lt;template&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageName }}&lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageVersion }}&lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageUser }}&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { inject } from "vue";
export default {
  setup() {
    let pageProperties = inject("pageProperties");
    return {
      pageProperties,
    };
  },
};
&lt;/script&gt;</code></pre>
<h4>The full working example</h4>
<p><iframe style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;" title="vue-3-basics-program-easily" src="https://codesandbox.io/embed/github/programeasily/vue-3-basics/tree/main/?fontsize=14&amp;hidenavigation=1&amp;view=split&amp;initialpath=provideandinjectsetupdemo&amp;module=%2Fsrc%2Fcomponents%2Fprovideinject-setup%2FPage.vue&amp;theme=dark" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start">﻿</span></iframe></p>
<h2>Provide And Inject in Vue.js with Reactivity</h2>
<p>In the above example, pageProperties will not be reactive by default. Moreover, They are not reactive by nature. <strong>We have to use <em>ref/reactive</em> with <em>provide</em> to make it reactive.</strong></p>
<p>Note that, In the below example we are using <em>reactive</em> with <em>provide</em> to make pageProperties reactive. Here updateVersion method is provided to all it&#8217;s children with pageProperties.</p>
<pre><code class="language-javascript">Page.vue

&lt;template&gt;
  &lt;div /&gt;
  &lt;!-- &lt;AppBar /&gt; 
  // Here we supposed to pass pageProperties
  // to Layout and Layout -&gt; Main Floor -&gt; Header Room
  // But with the help of provide/inject we can directly
  // use this in Header Room component --&gt;
  &lt;Layout /&gt;
&lt;/template&gt;

&lt;script&gt;
import { provide, reactive } from "vue";
import Layout from "./Layout";
export default {
  components: {
    Layout,
  },
  setup() {
    let pageProperties = reactive({
      pageName: "Provide/Inject",
      pageVersion: "1.0.0",
      pageUser: "admin",
    });
    provide("pageProperties", pageProperties);
    provide("updateVersion", () =&gt; {
      pageProperties.pageVersion = "2.0.0";
    });
    return {
      pageProperties,
    };
  },
};
&lt;/script&gt;</code></pre>
<p>After that, we are going to update the version from HeaderRoom Component with the help of <em>inject</em> directly.</p>
<pre><code class="language-javascript">HeaderRoom.vue

&lt;template&gt;
  &lt;div&gt;
    &lt;button @click="updateVersion"&gt;Update Parent Version&lt;/button&gt;
  &lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageName }}&lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageVersion }}&lt;/div&gt;
  &lt;div&gt;Page Name : {{ pageProperties.pageUser }}&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import { inject } from "vue";
export default {
  setup() {
    let pageProperties = inject("pageProperties");
    let updateVersion = inject("updateVersion");
    return {
      pageProperties,
      updateVersion,
    };
  },
};
&lt;/script&gt;</code></pre>
<p>So we can update parent component data(pageProperties version) from the child component using <em>provide</em> and <em>inject</em> concepts in Vue.js effectively.</p>
<h4>The full working example</h4>
<p><iframe style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;" title="vue-3-basics-program-easily" src="https://codesandbox.io/embed/github/programeasily/vue-3-basics/tree/main/?fontsize=14&amp;hidenavigation=1&amp;view=split&amp;initialpath=provideandinjectreactivedemo&amp;module=%2Fsrc%2Fcomponents%2Fprovideinject-reactive%2FPage.vue&amp;theme=dark" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start">﻿</span></iframe></p>
<h2>Global Provider &#8211; Provide Application API</h2>
<p>However, we can use <em>provide</em> as a global provider to all components. <strong>The provided data would be injected to all it&#8217;s components within the application.</strong> It&#8217;s an alternative way for <a href="https://v3.vuejs.org/api/application-config.html#globalproperties" target="_blank" rel="noopener">global properties</a> in Vue.js.</p>
<p>Let me show you the example below,</p>
<p>index.js</p>
<pre><code class="language-javascript">const app = Vue.createApp({});
// Global Provider
app.provide("appProperties", {
  user: "admin",
  appVersion: "1.0.0"
});
// Page Component where contains content component
app.component("Page", {
  template: `&lt;Content /&gt;`
});
// Content component where inject used to get
// global properties
app.component("Content", {
  inject: ["appProperties"],
  template: `
    &lt;div&gt;User Name : {{ appProperties.user }}&lt;/div&gt;
    &lt;div&gt;Version : {{ appProperties.appVersion }}&lt;/div&gt;
  `
});</code></pre>
<p>index.html</p>
<pre><code class="language-markup">&lt;div id="app"&gt;
      &lt;Page /&gt;
&lt;/div&gt;</code></pre>
<p>Here the <code>appProperties</code> is provided to all components within the application. We got the data in the <strong>Content</strong> component using <em>inject</em>.</p>
<h4>The full working example</h4>
<p><iframe style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden;" title="global-provider-provide-inject-vuejs" src="https://codesandbox.io/embed/github/programeasily/global-provider-example-vue/tree/main/?fontsize=14&amp;hidenavigation=1&amp;theme=dark&amp;view=split" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start">﻿</span></iframe></p>
<h2>Conclusion</h2>
<ul>
<li>In conclusion, <strong><em>Provide</em> and <em>inject</em> makes ease of data communication between parent and it&#8217;s child components.</strong></li>
<li><em>Provide</em> and <em>Inject</em> are not reactive by nature. But we can make it reactive with the help of <em>reactive/ref</em>.</li>
<li>We can use <em>provide</em> as global provider instead of global properties in Vue.js</li>
</ul>
<p>The post <a rel="nofollow" href="https://programeasily.com/2021/04/17/provide-and-inject-in-vue-js/">Provide And Inject in Vue.js</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://programeasily.com/2021/04/17/provide-and-inject-in-vue-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">37</post-id>	</item>
		<item>
		<title>Ref vs Reactive in Vue 3</title>
		<link>https://programeasily.com/2020/12/17/ref-vs-reactive-in-vue-3/</link>
					<comments>https://programeasily.com/2020/12/17/ref-vs-reactive-in-vue-3/#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Thu, 17 Dec 2020 18:55:05 +0000</pubDate>
				<category><![CDATA[Vuejs]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[vuejs]]></category>
		<category><![CDATA[vuejs3]]></category>
		<guid isPermaLink="false">http://programeasily.com/?p=31</guid>

					<description><![CDATA[<p>Today in the modern world, many JavaScript frameworks having their own reactive engines. Among them Vue&#8217;s defined special features is reactive system. The plain JavaScript object changes are tracked and when we modify it, the view gets updated. In Vue the reactive conversion affects all its nested children properties. Ref and Reactive both provides a way to store object data and allow that object to be reactive. So today we are going to discuss how this two helps and plays...</p>
<p class="read-more"><a class="btn btn-default" href="https://programeasily.com/2020/12/17/ref-vs-reactive-in-vue-3/"> Read More<span class="screen-reader-text">  Read More</span></a></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2020/12/17/ref-vs-reactive-in-vue-3/">Ref vs Reactive in Vue 3</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Today in the modern world, many JavaScript frameworks having their own reactive engines. Among them Vue&#8217;s defined special features is reactive system. The plain JavaScript object changes are tracked and when we modify it, the view gets updated.</p>



<p>In Vue the reactive conversion affects all its nested children properties. <strong>Ref and Reactive</strong> both provides a way to store object data and allow that object to be reactive. So today we are going to discuss how this two helps and plays major rule in a Vue&#8217;s reactive world. Lets take a deep dive!</p>



<figure class="wp-block-image size-large is-style-default"><img decoding="async" width="640" height="425" src="https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920-1024x680.jpg?resize=640%2C425" alt="ref vs reactive" class="wp-image-56" srcset="https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?resize=1024%2C680&amp;ssl=1 1024w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?resize=300%2C199&amp;ssl=1 300w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?resize=768%2C510&amp;ssl=1 768w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?resize=1536%2C1020&amp;ssl=1 1536w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?resize=407%2C270&amp;ssl=1 407w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?w=1920&amp;ssl=1 1920w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/computer-1245714_1920.jpg?w=1280&amp;ssl=1 1280w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /><figcaption>Ref vs Reactive in Vue3</figcaption></figure>



<h2 class="wp-block-heading">Ref</h2>



<p>It takes an primitives argument and return a reactive mutable object. The object has single property &#8216;value&#8217; and it will point to the argument taken by it.</p>



<pre class="wp-block-preformatted">const increment = ref(0);
console.log(increment.value) // 0

<code>increment.value++</code>;
<code>console.log(increment.value) // 1</code></pre>



<h3 class="wp-block-heading" id="block-e31ff1f9-e784-410e-a0ff-cb051b74ba8b">Within Template</h3>



<p>when we access <strong>ref</strong> inside template, it will unwrap the inner value automatically. No need to append .value in the template. <strong>Ref</strong> should be return as a property of setup hook(render context).</p>



<pre class="wp-block-preformatted">&lt;template&gt;
&nbsp;&nbsp;&lt;div&gt;{{&nbsp;state&nbsp;}}&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
<em>import</em>&nbsp;{&nbsp;ref&nbsp;}&nbsp;<em>from</em>&nbsp;'vue';
&nbsp;&nbsp;<em>export</em>&nbsp;<em>default</em>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;<em>setup</em>()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>return</em>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state:&nbsp;<em>ref</em>('Hello&nbsp;World...')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&lt;/script&gt;</pre>



<h3 class="wp-block-heading">Within Reactive Object</h3>



<p>when we pass <strong>ref</strong> as a argument to reactive object, it automatically unwraps its inner value. <strong>ref</strong> unwrapping won&#8217;t happens for Array, Map.</p>



<pre class="wp-block-code"><code>const increment = ref(0)
const state = reactive({
  increment
})

console.log(state.increment) // 0

state.increment = 1
console.log(count.increment) // 1</code></pre>



<h2 class="wp-block-heading">Reactive</h2>



<p>It takes a JavaScript object as a argument and returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy">Proxy</a> based reactive copy of the object.</p>



<pre class="wp-block-code"><code>const state = reactive({
name : 'john',
age  : 21
});</code></pre>



<p>we have to note that, the returned proxy is not same as original object. so in-terms of identity === comparison will be failed.</p>



<h3 class="wp-block-heading">Reactive within template</h3>



<pre id="block-92a41b9f-e6ad-4411-968b-82d66ef4ca26" class="wp-block-preformatted">&lt;template&gt;
&nbsp;&nbsp;&lt;div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&gt;&nbsp;User&nbsp;Name&nbsp;:&nbsp;{{&nbsp;<em>state.</em>name&nbsp;}}&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&gt;Age&nbsp;:&nbsp;{{&nbsp;<em>state.</em>age&nbsp;}}&lt;/div&gt;
&nbsp;&nbsp;&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
<em>import</em>&nbsp;{&nbsp;reactive&nbsp;}&nbsp;<em>from</em>&nbsp;'vue';
<em>export</em>&nbsp;<em>default</em>&nbsp;{
&nbsp;&nbsp;<em>setup</em>()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;const<em>&nbsp;state&nbsp;</em>=<em>&nbsp;reactive</em>({
<em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name</em>:<em>&nbsp;</em>"john",
<em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age</em>:<em>&nbsp;</em>21,
<em>&nbsp;&nbsp;&nbsp;&nbsp;</em>});
&nbsp;&nbsp;&nbsp;&nbsp;<em>return</em>&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state,
&nbsp;&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;},
};
&lt;/script&gt;
</pre>



<h2 class="wp-block-heading">Ref vs Reactive</h2>



<p>Typically, <strong>ref</strong> and <strong>reactive</strong> both have been used to create reactive objects where <strong>ref</strong> is used to make the primitive values to be reactive (<em>Boolean, Number</em>, <em>String</em>). But <strong>reactive</strong> won&#8217;t work with primitives rather than it works for objects.</p>



<p>Here the problem with <strong>reactive</strong> is the returned object of setup hook cannot be destructured or spread. To solve this issue Vue introduced <em><strong>toRefs</strong></em> which converts each property on a reactive object into ref object. So we have to remember to use <em><strong>toRefs</strong></em> with <em>reactive</em> when we returning the <em>reactive</em> proxy object from setup hook (composite functions)</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="640" height="303" src="https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/refreactive.png?resize=640%2C303&#038;ssl=1" alt="Difference Between Ref and Reactive in Vue3" class="wp-image-67" title="Ref Vs Reactive - Vue3" srcset="https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/refreactive.png?w=957&amp;ssl=1 957w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/refreactive.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/refreactive.png?resize=768%2C364&amp;ssl=1 768w, https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/refreactive.png?resize=570%2C270&amp;ssl=1 570w" sizes="auto, (max-width: 640px) 100vw, 640px" data-recalc-dims="1" /><figcaption>Ref vs Reactive &#8211; Vue3</figcaption></figure>



<h2 class="wp-block-heading">Summary</h2>



<p>So <strong>ref / reactive</strong> both are been used to create reactive object where the changes been tracked. The DOM will get updated only when there is a change in reactive objects. This is the fundamental understanding!. </p>



<p></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2020/12/17/ref-vs-reactive-in-vue-3/">Ref vs Reactive in Vue 3</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://programeasily.com/2020/12/17/ref-vs-reactive-in-vue-3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">31</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using disk: enhanced 
Minified using disk
Database Caching using disk (Request-wide modification query)

Served from: programeasily.com @ 2025-06-23 16:18:34 by W3 Total Cache
-->