What is the best way to adjust a component to display varying content based on the scenario?

I am looking for a way to reuse a component on my website that displays messages. The challenge is that in one section, the content consists of only a title and paragraph, while in another section, there are multiple titles and content pieces. I have implemented a solution that works, but I'm not sure if it's the most efficient approach. Is there a better way to achieve this? Here is the code snippet:

App.vue

<template>
  <div id="app" class="raleway">
    <link href='https://fonts.googleapis.com/css?family=Raleway' rel='stylesheet'>
    <section>
        <Message title="Just a title" messageContent="Just a message"/>
    </section>
  </div>
</template>

<script>
import Message from "./components/Message.vue";
export default {
  name: "App",
  components: {
    Message,
  },
};
</script>

<style>
html,
body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  background-color: #3B3B3B;
  font-family: 'Raleway';
}

</style>

Component

<template>
  <div class="container">
      <section>
        <h2>{{title}}</h2>
        <p>{{messageContent}}</p>
        <h2>{{title2}}</h2>
      </section>
  </div>
</template>

<script>
export default {
  name: 'Message',
  props: {
    title: String,
    messageContent: String,
    title2: String,
    messageContent2: String,
  },
}
</script>

<style scoped>
.container{
    text-align: center;
    margin: 0 auto;
    margin-top: 15em;
    color: #FFF;
    width: 82%;
}

.container h2{
    font-size: 2em;
    margin-bottom: .5em;
}

.container p{
    font-weight: 600;
    font-size: 1.5em;
    margin-top: 0;
}

@media only screen and (max-width: 768px) {
.container p{
    font-size: 1.33em;
}
}

@media only screen and (max-width: 580px) {
.container{
    margin-top: 12em;
}
.container h2{
    font-size: 1.17em;
}
.container p{
    font-size: 1em;
}
}
</style>

Answer №1

There are a couple of ways to approach this. One method involves utilizing an array of titles and messages as props.

<template>
  <div class="container">
    <section>
      <h2 v-for="(title, i) in titles">{{ title }}</h2>
      <p v-for="(messageContent, i) in messageContents">{{ messageContent }}</p>
    </section>
  </div>
</template>

<script>
export default {
  name: 'Message',
  props: {
    titles: {
      type: Array,
      default: () => []
    },
    messageContents: {
      type: Array,
      default: () => []
    },
  },
}
</script>

You can then incorporate this MessageContent component as shown below.

<template>
  <div id="app" class="raleway">
    <link href='https://fonts.googleapis.com/css?family=Raleway' rel='stylesheet'>
    <section>
      <Message :titles="['Title 1', 'Title 2']" :messageContents="['message 1', 'message 2']"/>
    </section>
  </div>
</template>

<script>
import Message from "./components/Message.vue";
export default {
  name: "App",
  components: {
    Message,
  },
};
</script>

Alternatively, you can also consider using slots for added flexibility, depending on the structure of the Message component.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Intriguing Riddle of HTML Semantic

I've been working on a puzzle presented in the link provided below: HTML Structure Challenge This mind-teaser consists of three questions: Modify the HTML code of the website to enhance its structure as follows: Replace the generic outer div eleme ...

Issue with Async pipe when utilizing autocomplete functionality

HTML Code <mat-form-field> <input type="text" matInput class="formControl" [formControl]="name" [matAutocomplete]="auto" > <mat-autocomplete #auto="matAutocomplete"> <mat-option *ngFor="let option of city | async" [valu ...

What is preventing the data property from updating in setInterval?

Looking for a way to increase the speed of the props while my marker is moving? Currently, the speed only updates when the pause button is clicked. How can I automatically update this.speed when clicking the speed button? I have defined the speed prop in ...

Customize Bootstrap radio buttons to resemble standard buttons with added form validation styling

Is there a way to style radio buttons to look like normal buttons while maintaining their functionality and form validation? I have two radio buttons that need styling but should still behave like radio buttons. Additionally, I am utilizing Bootstrap for s ...

Evolution of Vue's v-for feature from version 1 to version 2

Currently, I am in the process of learning Vue.js and stumbled upon a fantastic fiddle that perfectly showcases what I want to achieve. To view the fiddle yourself, click here: https://jsfiddle.net/os7hp1cy/48/ After integrating this fiddle into my proje ...

Undefined value is returned for Vue 3 object property

Is there a way to extract additional attributes from the Keycloak object ? Currently, If I try, console.log(keycloak) it will display the entire keycloak object. Even after reloading, it remains in the console. However, when I do, console.log(keycloak.t ...

Steps for implementing remote modals in Bootstrap 5 using CS HTML

I'm attempting to implement a remote modal window in Bootstrap 5 with C# MVC. The endpoint for the modal partial view is configured correctly. According to this discussion on Github, all that needs to be done is to call some JavaScript. However, it ...

Incorporating language modifications using the Laravel Vue Internationalization package

Hey there, I'm exploring Vue and attempting to create a multilingual application. During my research, I came across this package - https://github.com/xiCO2k/laravel-vue-i18n. I successfully installed it and managed to establish the connection for tran ...

Tips for Passing Parameters to Vuex mapActions correctly:Executing mapActions in Vuex requires a specific

I am facing an issue with my ProjectPage.vue where I display project issues in a v-data-table. The projects are fetched from a server API call in the sidebar and displayed there. Once I select a project, I want to use its id to retrieve its corresponding i ...

Assigning a value to an input field using jQuery

I'm facing an issue while trying to set the value for an input using jQuery. Essentially, when you click on a link, it redirects you to a different page with a form, and I am attempting to set the value of one of the inputs in that form. Unfortunately ...

Is it possible to simultaneously use the same plugin with various configurations and unique names in Vue?

I'm attempting to utilize the Vue Currency Input plugin, specifically with two different configurations simultaneously: import Vue from 'vue' import VueCurrencyInput from 'vue-currency-input' const options = { globalOptions: { ...

Guide on programmatically redirecting a user to a different route

I am currently working on a VueJS 3 application using Quasar, and I am facing difficulties with programmatically utilizing the Router. The issue can be summarized as follows: 1. The Challenge When attempting to navigate the User to a different route, onl ...

Tips for integrating bootstrap code within PHP tags

<?php $con=mysqli_connect("localhost","root","","testdatabase"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_qu ...

Vue.js: The Power of Manipulating Strings

Why is the filter method not working with this.userId, but it is working with the hard-coded value "admin"? How can I resolve this issue? computed: { UidMessages: function() { return this.Messages.filter(function(m) { return m ...

Pressing the button results in no action

I am currently developing a program that randomly selects 5 words from a database and inserts them into an array. Although the page loads correctly initially, nothing happens when the button is clicked. None of the alerts are triggered, suggesting that the ...

Is it possible to modify the background color of the firefox address bar according to the type of protocol being used?

Is there a way to customize the background color of the address bar in Firefox according to different protocols? For example, using blue for https, orange for mixed content warning, green for ev-certificate, and red for invalid certificates. ...

Stable element placement in relation to its container

While dragging an element, it obtains a position: fixed. This particular element is located within a modal that is directly nested inside the body element. In the image below, the modal appears in gray, while the rest of the body is black, and the button ...

Populate User Interface Popover - Placement on Compact Display

I have been grappling with finding a solution to position the material ui popover beneath my anchor consistently, even on smaller screens. Take a look at this sandbox example. The current setup is working alright, but the issue is that the scroll is cont ...

I am seeking to modify the CSS of the parent component upon the loading of the child component in Angular

In my Angular blog, I have a root component with a navigation bar containing router links to create a single-page application. My goal is to darken the background around the link when the child component loads. Currently, the link highlights only when pres ...

Implementing text sliding effect upon hovering over an image using CSS

Is there a way to make text appear when hovering over an image, sliding in from the top and covering only 50% of the image height? Here's the code I have so far: <img style="background-color:#3b283e; float:left;" src="images/f1.jpg" /> <div ...