Table of Contents
- Create a CoffeePlan Component
- Create the CoffeePlan Component without Props
- Add Name Prop to the CoffeePlan Component
- Github Repositories
Create a CoffeePlan Component
On day 13, I started refactoring the HTML code into a reusable CoffeePlan
component. The component will accept a name
input and display the input value in the HTML template.
Starter HTML Code
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
<div class="plan">
<div class="description">
<span class="title"> The Single </span>
</div>
</div>
<div class="plan">
<div class="description">
<span class="title"> The Curious </span>
</div>
</div>
<div class="plan">
<div class="description">
<span class="title"> The Addict </span>
</div>
</div>
</div>
</div>
The following HTML block repeats four times except they have a title title. Therefore, it can extract into a Coffee
<div class="plan">
<div class="description">
<span class="title"> The Single </span>
</div>
</div>
Create the CoffeePlan Component without Props
Vue 3 application
- Create a new
components/CoffeePlan.vue
file and update the template
<script setup lang="ts"></script>
<template>
<div class="plan">
<div class="description">
<span class="title"> The Single </span>
</div>
</div>
</template>
- Import
CoffeePlan
toApp.vue
<script setup lang="ts">
import CoffeePlan from './components/CoffeePlan.vue'
</script>
<template>
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
<CoffeePlan />
<CoffeePlan />
<CoffeePlan />
<CoffeePlan />
</div>
</div>
</template>
- At this moment, we will see four CoffeePlan components with the title 'The Single'. Later, we will refactor this component to accept different titles.
SvelteKit application
- Create a new
lib/coffee-plan.svelte
file and update the template
<script lang="ts"></script>
<div class="plan">
<div class="description">
<span class="title"> The Single </span>
</div>
</div>
- export the new component from
lib/index.ts
export * from './coffee-plan.svelte';
- Import
CoffeePlan
to+page.svelte
<script lang="ts">
import CoffeePlan from '$lib/coffee-plan.svelte';
</script>
<template>
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
<CoffeePlan />
<CoffeePlan />
<CoffeePlan />
<CoffeePlan />
</div>
</div>
</template>
Angular 19 application
- Create a new
CoffeePlanComponent
ng g c coffeePlan
@Component({
selector: 'app-coffee-plan',
imports: [],
template: `
<div class="plan">
<div class="description">
<span class="title"> The Single </span>
</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CoffeePlanComponent {}
- Import
CoffeePlanComponent
toAppComponent
inapp.component.ts
import { CoffeePlanComponent } from './coffee-plan/coffee-plan.component';
@Component({
selector: 'app-root',
imports: [CoffeePlanComponent],
template: `
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
<app-coffee-plan />
<app-coffee-plan />
<app-coffee-plan />
<app-coffee-plan />
</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {}
Add Name Prop to the CoffeePlan Component
Vue 3 application
- Define a
name
prop with a default value and proper type.defineProps
is automatically imported, so we do not have to import it explicitly.
<script setup lang="ts">
defineProps({
name: {
type: String,
default: 'Default Plan',
},
})
</script>
<template>
<div class="plan">
<div class="description">
<span class="title"> {{ name }} </span>
</div>
</div>
</template>
- Pass Input Values to
CoffeePlan
Component
<script setup lang="ts">
import CoffeePlan from './components/CoffeePlan.vue'
import { ref } from 'vue'
const plans = ref(["The Single", "The Curious", "The Addict", "The Hacker"])
</script>
<template>
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
<CoffeePlan v-for="plan in plans" :key="plan" :name="plan" />
</div>
</div>
</template>
SvelteKit application
- Define a
name
prop with a default value and proper type.$props
can be used without any import.
<script lang="ts">
interface Props {
name: string
}
const { name = 'Default Plan'} = $props();
</script>
<div class="plan">
<div class="description">
<span class="title">{name}</span>
</div>
</div>
- Pass Input Values to
CoffeePlan
Component
<script lang="ts">
import CoffeePlan from '$lib/coffee-plan.svelte';
const plans = $state(['The Single', 'The Curious', 'The Addict', 'The Hacker']);
</script>
<template>
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
{#each plans as plan (plan)}
<CoffeePlan plan={name} />
{/each}
</div>
</div>
</template>
Angular 19 application
- Define a
name
signal input in theCoffeePlanComponent
.
@Component({
selector: 'app-coffee-plan',
imports: [],
template: `
<div class="plan">
<div class="description">
<span class="title">{{ name() }}</span>
</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CoffeePlanComponent {
name = input('Default Plan');
}
- Pass Input Values to
CoffeePlanComponent
import { CoffeePlanComponent } from './coffee-plan/coffee-plan.component';
@Component({
selector: 'app-root',
imports: [CoffeePlanComponent],
template: `
<div class="content">
<h1 class="title">Coffee Plans</h1>
<h2 class="subtitle">We travel the world to source the very best single origin coffee for you</h2>
<div class="plans">
@for (plan of plans(); track plan) {
<app-coffee-plan [name]="plan" />
}
</div>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
plans = signal(['The Single', 'The Curious', 'The Addict', 'The Hacker']);
}
We have successfully created a reusable CoffeePlan
component and imported it to App
component to display different plans.
Top comments (0)