JavaScript data is not appearing on the user interface

I'm encountering an issue displaying data from an array in a .js file on the frontend. Despite following the correct import procedures, the content remains blank. I'm unsure what could be causing this problem.

The javascript file:

  import lemonBottle from "./assets/lemon-bottle.webp";
  import garlicBottle from "./assets/garlic-bottle.webp";
  import rosemaryBottle from "./assets/rosemary-bottle.webp";
  import chiliBottle from "./assets/chili-bottle.webp";
  import delicateBottle from "./assets/delicate-bottle.webp";
  import mediumBottle from "./assets/medium-bottle.webp";
  import boldBottle from "./assets/bold-bottle.webp";

  export const products = [
  {
  id: "234",
  name: "Lemon",
  price: "$26.00",
  imageName: lemonBottle,
  },
  {
  id: "345",
  name: "Garlic",
  price: "$26.00",
  imageName: garlicBottle,
  },
  {
  id: "456",
  name: "Rosemary",
  price: "$26.00",
  imageName: rosemaryBottle,
  },
  {
  id: "567",
  name: "Chili",
  price: "$26.00",
  imageName: chiliBottle,
  },
  {
  id: "678",
  name: "Delicate",
  price: "$24.50",
  imageName: delicateBottle,
  },
  {
  id: "789",
  name: "Medium",
  price: "$24.50",
  imageName: mediumBottle,
  },
  {
  id: "890",
  name: "Bold",
  price: "$24.50",
  imageName: boldBottle,
  },
  ];

Below is the front-end code snippet:

    <template>
    <div class="shop mt-20 border mx-auto w-4/6">
    <div class="product-grid md:grid grid-cols-3 md:gap-10 mt-10 p-5">
    <div
    class="border shop-item mb-5"
    v-for="product in products"
    :key="product.id"
  >
    <img class="h-[450px] w-[350px]" :src="product.imageName" />

    <p class="mt-1 text-wrap text-left text-2xl text-gray-500 font-bold">
      {{ product.name }}
    </p>
    <p class="mt-1 text-wrap text-left text-3xl font-bold">
      {{ product.price }}
    </p>
  </div>
</div>
</div>
</template>

<script>
import { products } from "@/product-data";
console.log("Products:", products);
export default {
name: "ShopView",
data() {
return {
  products: [],
};
},
};
</script>

Unfortunately, the output appears blank and upon inspection of the page, the code within the <div shop-grid doesn't seem to render.

Answer №1

If you want to properly initialize the products array in your script section, follow these steps:

<script>
import { items } from "../data";
export default {
  name: "ProductView",
  data() {
    return {
      items: items, //make sure it's not an empty array
    };
  },
  mounted() {
    console.log("Items:", items);
  },
};
</script>

Simply importing the products array won't do the trick. Make sure to declare and assign values in the data section.

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

Troubleshooting problem with JS Hashchange Event on Internet Explorer (MSIE

I've been working on loading my WordPress website using AJAX and came across this helpful tutorial. Everything in the tutorial makes sense to me, but it references a plugin called JS Hashchange Event. The problem I encountered is that it utilizes $.br ...

Embed images in a 'v-data-table' dynamically

<v-data-table :headers="headers" :items="items" :search="search" hide-default-footer class="elevation-1" > <template #[`item.employee_avatar`]="{ item }"> <v-img :src=&q ...

Integrating dynamic transition effects using Jquery Mobile for <div> elements

Just a friendly reminder, I must admit that my knowledge of Javascript is quite limited. I received a script from Padilicious to implement swipe navigation on my Jquery Mobile Site. It involves adding functions to a div tag like this: <div data-ro ...

React app experiencing issues with Bootstrap offset functionality specifically when paired with a button element

I am struggling to position the button after two columns of space in this code snippet. Despite my efforts, the button remains pulled to the left and I can't seem to update it. { values.contact_persons_attributes.length < 2 && <div className= ...

Locating characters within a string using JavaScript

Currently, I am enrolled in JavaScript classes and have come across a challenging question. In this scenario, the task is to create a function named countLetters. This function will take two parameters: 1) sentence - this parameter should be of type stri ...

Leveraging the Power of React.js and Vue.js Side by Side within Laravel

I am curious to know if it is feasible to integrate both react.js and vue.js simultaneously within a Laravel project. For instance, using ReactJs for the admin dashboard and VueJs for the client dashboard. While it is typically recommended to stick with ...

Building an LED display with three.js

I have a project where I am creating a board with multiple RGB LEDs mounted on it, as shown in the image. https://i.sstatic.net/WtOm4.png To create the LEDs, I used the following code: this.light = new THREE.PointLight(color, intensity, distance, decay) ...

Where should I set a value to ensure all listeners are triggered?

In my AngularJS view, I have a dropdown that triggers a function when the color is changed: <select ng-change="shop.updateSizes()" ng-model="shop.color" ng-options="color.name for color in COLORS"></select> The updateSizes method in my contro ...

Explore the jQuery feature of Image Preview and modify the size of the Hover image

Can someone help me with a simple question? I have been using the Easiest Tooltip and Image Preview Using jQuery and also tried out this example Everything is working fine, but I need to adjust the hover image size in the tooltip. The current image is to ...

Is there a way to trigger the mouseover event on every element currently under the cursor?

In my web application, I have a feature where a dot is created every time you click. However, when hovering over a stack of dots, only the top dot triggers the mouseover or mouseenter event. How can I ensure that the events fire for every dot under the cur ...

Guide on sending JavaScript property Array to Jersey JAX-RS JSON-based RESTful web service

Is it possible to send an array of JS strings to the server side within a JS Object field? Here is an example: JSON: {prodName: "abc123", prodImages: ["a1", "a2", "a3"]} Currently, I am utilizing Jersey JAX-RS to handle the JSON input. The server-side c ...

Error encountered in jsonwebtoken payload

Working on a web application with nodejs and angular cli, I have implemented JWT for login authentication. However, during the processing, I encountered the following error: Error: Expected "payload" to be a plain object. at validate (D:\Mean ...

Retrieve the initial class of an element using either jQuery or JavaScript

I need help with a jQuery function that is not working properly. I have 3 anchors with corresponding divs, and when I click on one of the anchors, I want to display the contents of the corresponding div. The anchors are as follows: <a class="home share ...

Would it be feasible to activate internal routing for the displayed HTML content rather than having to refresh the entire page?

Receiving an HTML string from the server presents a challenge due to constraints in modifying its format, particularly in a legacy system. However, there is a need to display this HTML content in the DOM. To address this issue, the bypassSecurityTrustHtml ...

Activate the resizing feature for materializecss textareas once the text has been filled in

Imagine having the following code snippet: <div class="row"> <form class="col s12"> <div class="row"> <div class="input-field col s12"> <textarea id="textarea1" class="materialize-textarea"></te ...

What is the best way to showcase data from vue.js on a blade template?

I am working on a project using vue.js and laravel. I have a list of products, and when I click on the "Quick view" button for a specific product, I want to display all the data about that product in a modal. However, the data is not showing up in the moda ...

Tips for concealing JavaScript animations beyond specific boundaries?

So I'm delving into the world of javascript/jquery and an amazing idea popped into my head for a webpage effect. Let me break down the design a bit. My website is neatly contained within a wrapper div, ensuring that the content stays at 1000px and ce ...

When the enter key is pressed, shift the text to a different input field

I am facing a complex requirement How can I detect the pressing of the ENTER key within a text box with the following conditions: If the cursor is at the end of a sentence, it should create a new text box on the next row. If the cursor is in the middle ...

Top method in Angular 6 for verifying if a scrollable section has been scrolled to the very bottom

I am searching for a reliable solution in Angular 6 to determine whether a scrollable host component has reached its maximum scroll bottom. Despite my efforts, I have been unsuccessful in finding a functioning example of a function that can detect if a cu ...

Utilizing RabbitMQ's amqp.node integration within a Node.js Express application

The RabbitMQ Javascript tutorials feature the use of the amqp.node client library amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; ch.assertQueue(q, {durable: fal ...