The curly brackets in Vue.js do not function properly within a v-for loop when there is an object nested inside an array

I am a novice in vuejs and I am attempting to utilize mustache {{}} to render elements of an array. It works fine for simple arrays, but when I try it with an array containing objects, it doesn't seem to work.

Is there an issue with my code, or is it simply not possible and I need to use another method that I am not familiar with?


var app = new Vue({
  el: '#app',
  data: {
    object1: {
      type: "ball",
      color: "red",
      number: 1
    },
    object2: {
      type: "house",
      color: "blue",
      number: 2
    },
    items: [object1, "hello", "world", object2]
  }
})

@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Montserrat&display=swap');
@import url('https://db.onlinewebfonts.com/c/20af36ba0a52c9a46fe2963f400bd77c?family=Dystopian+Black');
body {
  margin: 0;
  background-color: #1b1c21;
  font-family: Mont;
  user-select: none;
}
...
...

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<!DOCTYPE html>
<html lang="fr">
...
...
...

Answer №1

Remember to use the keyword this when referencing a declared variable in the data object. Take a look at the example below:

var app = new Vue({
  el: '#app',
  data: {

    object1: {
      type: "ball",
      color: "red",
      number: 1
    },

    object2: {
      type: "house",
      color: "blue",
      number: 2
    },
    items: []

  },
  created() {
    this.items = [this.object1, "hello", "world", this.object2]
  }
})
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Montserrat&display=swap');
@import url('https://db.onlinewebfonts.com/c/20af36ba0a52c9a46fe2963f400bd77c?family=Dystopian+Black');
body {
  margin: 0;
  background-color: #1b1c21;
  font-family: Mont;
  user-select: none;
}

.header {
  padding: 10px 16px;
  background: #27282c;
  margin: 0;
}

.main {
  margin: 15px;
  color: white;
}

.header img {
  height: 75px;
  width: 75px;
  margin-inline: 260px;
  margin-block: 20px;
  transition: transform .5s, background-color .5s;
  padding: 10px;
  border-radius: 10px;
}

.header img:hover {
  transform: scale(120%);
  background-color: #4d4d4d;
  transition: transform .5s, background-color .5s;
}

.icon-top {
  width: 225px;
  height: 225px;
  display: inline-block;
  filter: drop-shadow(0 0 0.5rem #2fc967);
}

.title-top {
  font-size: 320px;
  font-family: 'Bebas Neue';
  color: white;
  margin: 0;
  display: inline-block;
}

span {
  color: #2fc967;
  filter: drop-shadow(0 0 0.5rem #2fc967);
}

p {
  margin: 0;
}

.top {
  margin-bottom: 70px;
  margin-top: 70px;
  margin-left: 87px;
  line-height: 60px;
}

.text-top {
  margin: 0;
  color: white;
  font-family: 'Bebas Neue';
  font-size: 85px;
  text-decoration: drop-shadow(0 0 0.5rem #2fc967);
  margin-left: 240px;
}

.title-white {
  color: white;
}

.title-black {
  color: black;
}

.cards {
  float: left
}

.card {
  border-radius: 50px;
  background: #1b1c21;
  box-shadow: 15px 15px 30px #111215, -15px -15px 30px #25262d;
  width: 300px;
  height: 300px;
  margin: 80px;
  transition: transform .5s;
  display: inline-block;
  justify-content: center;
  justify-items: center;
}

.card:hover {
  transform: scale(1.1);
}

.card img {
  width: 75%;
  height: 75%;
  margin-inline: 12.5%
}

.card p {
  color: white;
  font-family: "Dystopian Black";
  font-size: 35px;
  text-align: center;
  line-height: 55px;
}

a {
  margin: 0 auto;
  padding: 0 auto;
  text-decoration: none;
}

::-webkit-scrollbar {
  width: 10px;
}

::-webkit-scrollbar-thumb {
  background: #2fc967;
  border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<!DOCTYPE html>

<html lang="fr">

<head>
  <link rel="stylesheet" href="css/style.css">
  <link rel="icon" href="images/icon.svg">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>xxxxx</title>
  <script src="js/vue.js"></script>
</head>

<body>
  <div class="app" id="app">
    <div class="top" id="top">
      <div class="title-top">
        <p>xxxxx<span>.</span></p>
      </div>
      <div class="text-top">
        <p>xxxxxx</p>
      </div>
    </div>
    <div class="header" id="header">
      <a href="xxximg src=" xxx " alt="Cours "></a>
            <a href="xxximg src="xxx" alt="Parcours"></a>
      <a href="xxx"><img src="xxx" alt="À propos"></a>
    </div>
    <div class="main" id="main">
      <div class="cards" v-for="item in items">
        <a href="https://youtube.com">
          <div class="card">
            <p>{{item}}</p>
          </div>
        </a>
      </div>
    </div>
  </div>

  <script src="js/script.js"></script>
</body>

</html>

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

Unable to align divs horizontally

On my webpage, I have set up two divs - Box1 and Box2. Within Box2, there are two nested divs called Box2-1 and Box2-2. However, when I view the page, Box1 and Box2 do not align side-by-side as expected. Instead, Box1 moves downwards and lines up with the ...

Zooming on a webpage is causing problems

One issue I'm facing is with the elements on my page acting strange when I zoom in and out. Everything seems to be working fine except for a container div that should overlay a color over the image background. When I zoom in or switch to mobile view, ...

Can you answer this straightforward query about CSS positioning?

I am currently facing a challenge in creating a small div (header class) that overlays a main banner image div (banner class). When I maximize my browser window, the positioning is correct. However, upon resizing the browser, the header div maintains its m ...

When combining Symfony, Twig, and Semantic UI, users may encounter an issue where the background image fails to display

Can someone assist me in identifying the issue with my code? I am using Symfony 2.8, Semantic UI, and Twig, attempting to set a background image on my site. The modification is being made to my base.html.twig file. I have attempted: Using different ima ...

Choosing elements that are visible on the webpage using CSS selectors or XPath

The CSS selector for the parent element (which is always visible) can be found at this XPath: /html/body/section/div/div[2] After a few seconds (due to some JavaScript), one of its children will become visible. I need to locate and select this visible ...

Steps for retrieving the URL using the getDownloadURL() method

After exploring various options, I have come across multiple solutions but none seem to be suitable for my specific requirements! I have managed to successfully upload a photo to Firebase along with all necessary information. However, when attempting to r ...

Redirect the Submit button to the specified URL from the form element

Looking for a way to create a form with two fields: Username and Password. Upon clicking submit, the username and password should be added to the URL in a specific format. For instance: The URL structure will remain the same, only the "username_entered_i ...

Nuxt.js framework and Three.js library are throwing a SyntaxError: Unable to utilize an import statement outside of a module

Utilizing Nuxt.js to develop a static landing page and integrating Three.js for rendering a 3D Globe on the page. It works flawlessly in both dev and production modes locally, but upon deploying to netlify using yarn generate to create a full static site, ...

The vue.config.js file is throwing an error due to incorrect options: "build" and "dev" are not permitted in this context

I have successfully upgraded my vue-cli 2 app to vue-cli-3. In the process, I added my custom dependencies and seamlessly integrated the src/ folder into the new vue-cli-3 app. Exciting! Now that we are using vue.config.js, how should I handle [PROD|DEV|T ...

The Joys of Delayed Animation with jQuery Isotope

Has anyone successfully modified the CSS3 transitions in jquery Isotope to incorporate a delay for shuffling items, such as using "transition-delay: 0s, 1s;"? I am aiming for a specific shuffle direction - first left, then down - to create a more calculat ...

Remove HTML formatting from excel cells and fill separate cells

Is there a way to condense HTML code and fill different columns in Excel? For instance, If my HTML code looks like this: <p></p>10-16-2013 22:35<br/>I love pizza! Ordering was a breeze!<p></p>10-16-2013 13:19:46<br />t ...

What is the best way to ensure that an input field and a button stretch across the entire row in a Bootstrap form?

I need assistance with aligning the Post button to the right and having the text field for tags occupy the entire left section of the row within a Bootstrap 3 form. The form markup is shown below: https://i.sstatic.net/401RU.png Below is the complete cod ...

Overlapping Bootstrap rows in small screens

I'm currently working on a frontend project using bootstrap 5, specifically utilizing the grid system to ensure responsiveness across different devices. However, I've encountered an issue in my code that I can't quite figure out. The probl ...

Issue with Bootstrap flash alert CSS on mobile devices is not functioning properly

I am in the process of developing a Rails application. Within my app, I utilize Bootstrap's CSS for displaying flash messages on alerts. Instead of fully incorporating Bootstrap's CSS, I only integrate styles related to alerts in order to prevent ...

Utilizing BeautifulSoup for Web Scraping (handling missing values during the scrape)

I have been facing two challenges while trying to scrape data from a realtor website using BeautifulSoup. These issues have proven to be quite tricky: Challenges: After running my code snippet, I noticed that some date values are missing in the dataframe ...

Issues with Google Fonts failing to load correctly

Would you mind taking a look at this issue for me? In all browsers except Firefox, the expected behavior is that a web font remains invisible until fully loaded, preserving space on the page. Interestingly, in Chrome and IE9 (the browsers I tested), ther ...

HTML 4.01 character and character offset attributes

I'm curious about the purpose of the charoff attribute in HTML 4.01 Transitional. Specifically, consider a table column that contains the following cells: <td>1.30</td> <td>10</td> <td>100.2</td> <td>1000 ...

Applying the active state to the link will cause it to inherit the default styles of the

I am facing a challenge with overriding the active style of links in a universal manner, to restore them to their original state when they cannot be interacted with (such as during scrolling). In my current code, I implemented this: .scrolling a:active { ...

Guide on executing edit form alongside file upload

Is there a way to update a form without having to upload a new image? I have successfully implemented multer for image uploads when creating a form. My frontend is built using ReactJS and the backend server side is Node.js. This is the front end code usi ...

Exploring Vue.js: Navigating through child components from parent component

I currently have 3 components in my project: Form, Card, and Button. Let's start with the Button.vue component: <template> <div> <slot v-bind="{ text }"> <button>{{ text }}</button> </slot> ...