Issue with Vue template not displaying within a loop

After completing a basic Vue tutorial on setting up a Todo app, I decided to integrate some aspects of it into a website I am developing. However, I have encountered an issue with my for-loop that is not functioning as expected.

The project was initially set up using vue-cli, and the majority of the code was copied from the tutorial (which works flawlessly with its own for-loop).

It appears that the data may not be properly passed to the template.

I have attempted the following troubleshooting steps:

  1. Including the information within the props and data sections
  2. Passing both the entire object and individual parameters to the template
  3. Trying hard-coded values inside the array being iterated upon

(After establishing a new vue-cli project:)

App.vue:

<template>
  <div id="app">
    <create-section v-on:create-section="addSection" />
    <section v-for="section in sections" v-bind:key="section.title" :info="section"></section>
  </div>
</template>

<script>
import CreateSection from "./components/CreateSection";
import Section from "./components/Section";
export default {
  name: "App",
  components: {
    CreateSection,
    Section
  },
  data() {
    return {
      sections: []
    };
  },
  methods: {
    addSection(section) {
      this.sections.push({
        title: section.title,
        description: section.description
      });
      console.log(
        "Added to sections! : " + section.title + " | " + section.description
      );
      console.log("Sections length: " + this.sections.length);
    }
  }
};
</script>

Section.vue

<template>
  <div class="ui centered card">
    <div class="content">
      <div class="header">{{ info.title }}</div>
      <div>{{ info.description }}</div>
    </div>
  </div>
</template>


<script type = "text/javascript" >
export default {
  props: {info: Object},
  data() {
    return {};
  }
};
</script>

Expected outcome: The Section template should be displayed on the website after creating it with the addSection method called by another script. The addSection method is not included here for brevity.

Actual outcome: No content is displayed, only an empty tag is added.

Answer №1

It appears that the issue lies in naming your component Section. Since <section> is a standard HTML element, using it as a component name will cause conflicts.

The library does have a case-sensitive warning, but it can be misleading. To resolve this, adjust your components section to:

components: {
  CreateSection,
  section: Section
},

This adjustment should trigger the warning message for clarity.

To correct this issue, simply rename the component to something else.

You can find more information about this in the initial entry of the style guide here:

https://v2.vuejs.org/v2/style-guide/#Multi-word-component-names-essential

Answer №2

While the HTML5 element 'section' already exists, it is recommended to choose a different name for your section component.

If you insist on naming the component Section, consider registering it as 'v-section'

Answer №3

The issue arises when attempting to loop through the `sections` array using

<section v-for="section in sections" v-bind:key="section.title" :info="section"></section>
. This is because the `sections` array is initially empty, and therefore adding new elements to it requires triggering a computed property in order to populate the data for the `section` component.

Answer №4

When working with Vue components, it's important to avoid using existing HTML5 commands as names for your components. Make sure to choose a unique name for your Vue component to prevent conflicts. Additionally, make sure to declare props correctly in your components like shown below:

<script type = "text/javascript" >
export default {
  props: ['info'],
  data() {
    return {};
  }
};
</script>

Props should receive the name of the property from the parent component and be defined as strings.

I hope this information is helpful to you!

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

Dropdown with multiple selections closes after making only one selection

Currently, I have a multi-select dropdown feature where users can list and select multiple items at once. Here is the code snippet I am utilizing for this functionality: <Multiselect v-model="abc" valueProp="x ...

Tips for correctly linking JS and CSS resources in Node.js/Express

I have a JavaScript file and a stylesheet that I am trying to link in order to use a cipher website that I created. Here is my File Path: website/ (contains app.js/html files and package json) website/public/css (contains CSS files) website/public/scri ...

The MongoDB GridFS is refusing to accept the buffer being written

Hey everyone, I've been working on this issue for nearly a day now and can't seem to figure it out. I'm utilizing multer's inMemory flag to upload an image from my website. My approach involves writing the buffer received from multer to ...

Simplifying complex JSON structures by un-nesting arrays

Within my Formik form, I have 3 fields: MemberMemberID, EventEventID, and event_date. This form represents an event (such as a Tuesday club) taking place on a specific date and attended by various members. Formik stores the data in key-value pairs within ...

Error Message: A key is being provided to the classes property that is not implemented in the current context

Trying to customize Material-UI styles with makeStyles() as outlined in the documentation but encountering a warning when passing a classname in the parent component that is not specified in useStyles. The warning message reads: Warning: Material-UI: th ...

Encountering a TypeScript error while calling a Vue lifecycle hook method

Struggling to call a method in a Vue root component from a lifecycle method in typescript? See below for a simple example that showcases this issue: import Vue from "vue"; class Game { a: number; b: number; constructor() { this.a = 3; ...

Challenges arising from the intersection of Vue's scoped styles and Bootstrap

Currently, I am working on developing an embedded plugin using Vue.js. During the development phase, this plugin is integrated into a html page filled with placeholder text that loads Bootstrap. I recently discovered that one of the elements within my plu ...

I am currently working on creating a navigation bar for a webpage using the express framework and pug library. However, when I navigate to the demo page endpoint, the screen appears blank and nothing is displayed

//In the following JavaScript code, I am trying to implement basic routing navigation using express. However, when I try to insert HTML into the demo page, nothing appears on the browser screen. const path = require("path"); const app = ...

The content in tinymce cannot be edited or removed

Is there a method to prevent certain content within the tinyMCE Editor from being edited or removed? While I know that adding a class "mceNonEditable" can make a div non-editable, it can still be deleted. Is there a way to make it unremovable as well? ...

Exploring the power of async/await in combination with map or foreach

I am facing a challenge in retrieving multiple product prices from my database. I had initially thought of using the map or forEach methods to iterate through them and add up the prices to a variable as shown below: // Get Total exports.getTotal = (req,re ...

An issue occurred in the callback function for the watcher "get_settings": "A TypeError was raised because the property 'general' cannot be read

I'm really struggling with handling this error in Vue, especially since I'm new to it. Here's what's happening: I fetch data from the server using a vuex store action. In my component, I access that data using a getter in a computed pro ...

What is the best way to distribute components between two NextJS projects?

Confused about the best way to share ReactJs components between two NextJs applications - one for e-commerce and the other for a manager portal. In the e-commerce app, there are various UI components that I want to reuse in the manager portal. Considering ...

Having difficulty updating the border color of Material UI Input when it is in focused or unfocused state

I can't seem to figure out why this code isn't working as expected. I'm trying to target the MuiInputBase-root element, specify that it should have a blue border by default, and then change the border color to red when focused. Can someone h ...

Utilize AJAX in JavaScript file

I am encountering an issue with the following code: function inicioConsultar(){ $(function(){ $('#serviciosU').change(function(){ if ($('#serviciosU').val()!= "-1") { $.ajax({ url: "@Url. ...

Incorporate interactive click buttons into your Telegram bot

Currently working on a telegram bot using node.js with the telegram-bot API by yagop. https://github.com/yagop/node-telegram-bot-api My goal is to allow users to stop typing messages and instead just click on buttons that will trigger actions. When a user ...

Setting up a basic testcafe configuration with ES modules

Unique Title When it comes to using ES modules with Testcafe, the documentation may not provide clear instructions on configuring global hooks. There seems to be limited options for configuring Testcafe globally, such as using .json or CommonJS. In my pr ...

What could be causing req.body to consistently come back as an empty object?

I am struggling with req.body always returning an empty object regardless of what I try. I have experimented with: var jsonParser = bodyParser.json(); and then including jsonParser in the function -> app.post('/api/get-last-project',jsonPar ...

Angular directive: Exploring the disparities between controller scope and link function scope

As I delve into the world of Angular directives, the concept of scope is proving to be quite challenging for me. Let's consider a custom directive named parentDirective, which has both a controller property and a link property: angular.module("app"). ...

Update the contents of a div element with JavaScript and PHP combo

When I click on the following div, I want to replace it with a similar one from file.php. The div tag in question is: <div style="display: none;" id="extra" class="container2" xml:id="page"> <div class="title"> <h2>Stuff to check out< ...

The $firebaseObject variable is not defined

My AngularJs + AngularFire setup includes email/password authentication and a node called "users" to list authenticated users. Here is a snapshot: https://i.sstatic.net/eBz3G.png Within my authentication service, I have an AngularJs factory: .factory(&ap ...