<?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/"
	>

<channel>
	<title>vue class component &#8211; Filip Raiper34 Gulan</title>
	<atom:link href="https://gulan.eu/tag/vue-class-component/feed/" rel="self" type="application/rss+xml" />
	<link>https://gulan.eu</link>
	<description>Senior frontend developer</description>
	<lastBuildDate>Fri, 07 Jul 2023 21:11:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Vue3 Router params in Class component</title>
		<link>https://gulan.eu/vue3-router-params-in-class-component/</link>
					<comments>https://gulan.eu/vue3-router-params-in-class-component/#respond</comments>
		
		<dc:creator><![CDATA[Raiper34]]></dc:creator>
		<pubDate>Sat, 15 Jan 2022 14:50:39 +0000</pubDate>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Typescript]]></category>
		<category><![CDATA[Vue]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[vue]]></category>
		<category><![CDATA[vue class component]]></category>
		<category><![CDATA[vue router params]]></category>
		<category><![CDATA[vue3]]></category>
		<category><![CDATA[vue3 router params in class component]]></category>
		<category><![CDATA[vuejs]]></category>
		<guid isPermaLink="false">https://gulan.eu/?p=241</guid>

					<description><![CDATA[<span class="span-reading-time rt-reading-time" style="display: block;"><span class="rt-label rt-prefix">Reading Time: </span> <span class="rt-time"> 2</span> <span class="rt-label rt-postfix">minutes</span></span>Recently I started to learn and program some simple demo applications (advertisement portal) in Vuejs. As an Angular developer, I am surprised by the number of ways Vue offers to achieve the desired goal. I think it is sometimes good, sometimes bad, to have multiple possibilities. Vue offers multiple approaches how to making components. Recently [&#8230;]]]></description>
										<content:encoded><![CDATA[<span class="span-reading-time rt-reading-time" style="display: block;"><span class="rt-label rt-prefix">Reading Time: </span> <span class="rt-time"> 2</span> <span class="rt-label rt-postfix">minutes</span></span>
<p class="has-text-align-left">Recently I started to learn and program some simple demo applications (advertisement portal) in Vuejs. As an Angular developer, I am surprised by the number of ways Vue offers to achieve the desired goal. I think it is sometimes good, sometimes bad, to have multiple possibilities. Vue offers multiple approaches how to making components. Recently Vue developers add a new feature called the Vue <strong><a rel="noreferrer noopener" href="https://class-component.vuejs.org/" target="_blank">Class component</a></strong>. It is the approach how to making Vue components using ES6 classes, it means using inheritance, methods, properties, and all OOP principles. It is great for me, It is more intuitive for me than plain Javascript composition API or options API. </p>



<p class="has-text-align-left">However, when there is the great <strong>Class component</strong> feature, documentation and guides are still written using old principles and it is hard for newcomers to join principles together. At least it was hard for me. I was completely lost on how to access router params of the <strong><a rel="noreferrer noopener" href="https://router.vuejs.org/" data-type="URL" data-id="https://router.vuejs.org/" target="_blank">Vue router</a></strong> in the <strong>Class component</strong> and how to define these params. I needed to experiment a bit because I was unable to find a solution that fits me. I mean Vue3 application using Typescript, Vue class component, and Vue router.</p>



<h2 class="wp-block-heading">Router params in class component simple solution</h2>



<p>Yes, it was hard for me, but the completed solution is quite simple. We need to define a route as follows: </p>



<pre class="wp-block-code"><code lang="typescript" class="language-typescript">{path: '/edit-advertisement/:id', name: 'Edit Advertisement', component: EditAdvertisement, props: true}</code></pre>



<p>We defined route param placeholder in the path using <strong>:id</strong> and also it is important to set <strong>props</strong> property to <strong>true</strong>. <strong>Props</strong> property means that the router parameter will be passed to the Vue component as a property (surprisingly). Then in your Vue class component, you can access it: </p>



<pre class="wp-block-code"><code lang="typescript" class="language-typescript">import { Options, Vue } from 'vue-class-component';

@Options({
  props: {
    id: String
  }
})
export default class EditAdvertisement extends Vue {

  mounted() {
    console.log(this.id);
  }
}</code></pre>



<p>We defined <strong>id</strong> prop in <strong>Options</strong> decorator. If you do this, you can access this router param in this class using <strong>this</strong> keyword. Exactly the same as normal component props. So if you type <strong>&lt;APP_URL&gt;/edit-advertisement/1</strong> you get <strong>1</strong> in your component.</p>



<p>As you can see, it was quite a simple solution, but when I started to learn Vue3 I was unable to do it because there were solutions on the internet and StackOverflow without the <strong>Vue class component</strong> and, to be honest, I was confused from the number of possibilities that did not work together.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://gulan.eu/vue3-router-params-in-class-component/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
