Issue with displaying a vTable component in VueJS / Vuetify

I am struggling with this basic HTML/Vue/Vuetify code snippet below, and I can't seem to get it functioning as intended.

const { createApp, computed, ref, reactive } = Vue;
            const { createVuetify } = Vuetify;
            const vuetify = createVuetify();
            const app = createApp({
                setup() {
                    const desserts = ref([{
                        name: 'Frozen Yogurt',
                        calories: 159,
                    }, {
                        name: 'Ice cream sandwich',
                        calories: 237,
                    }, {
                        name: 'Eclair',
                        calories: 262,
                    }, {
                        name: 'Cupcake',
                        calories: 305,
                    }, {
                        name: 'Gingerbread',
                        calories: 356,
                    }, {
                        name: 'Jelly bean',
                        calories: 375,
                    }, {
                        name: 'Lollipop',
                        calories: 392,
                    }, {
                        name: 'Honeycomb',
                        calories: 408,
                    }, {
                        name: 'Donut',
                        calories: 452,
                    }, {
                        name: 'KitKat',
                        calories: 518,
                    },])

                    return {
                        desserts
                    }
                }
            });
            app.use(vuetify).mount('#app');
<!DOCTYPE html>    
            <html>

            <head>
                <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e484b5b7e0d100d100a">[email protected]</a>/dist/vue.global.min.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/3.3.19/vuetify.js"></script>
                <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec9a998998858a95acdfc2dfc2dddb">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
                <link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8ded7d6ccf88d96c0">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">
            </head>

            <body>

                <div id="app">
                    <p>table below this point</p>
                    <v-table>
                        <thead>
                            <tr>
                                <th class="text-left">URL</th>
                                <th class="text-left"></th>
                            </tr>
                        </thead>
                       <tbody>
                            <tr v-for="item in desserts" :key="item.name">
                                <td class="url">{{ item.name }}</td>
                                <td class="title">{{ item.calories }}</td>
                            </tr> 
                        </tbody> 
                    </v-table>
                    <p>table over this point</p>
                </div>
            </body>

            </html>

I am trying to display some data within a simple <v-table></v-table> component (not the <v-data-table> from labs) but it's not rendering properly, throwing errors in the console:

  • [Vue warn]: Property "item" was accessed during render but is not defined on instance. at VTable at App
  • [Vue warn]: Unhandled error during execution of render function at VTable at App
  • Uncaught TypeError: Cannot read properties of undefined (reading 'name')

I have checked that the {{deserts}} property is passed to the app as I can see the array displayed on the page. When I remove the <tr for></tr> loop, the devTool shows the following HTML:

<div id="app" data-v-app="">
        <p>table below this point</p>
        <div class="v-table v-theme--light v-table--density-default">
            <!---->
            <div class="v-table__wrapper">
                <table> 
                    URL 
                </table>
            </div>
            <!---->
        </div>
        <p>table over this point</p>
    </div>
    

I need to resolve this using Vue and Vuetify fetched from a CDN as I'm working on an internal app within a WordPress page. I might have missed something, but I can't figure out what it is since all other components are functioning correctly... Been stuck on this issue for days now, even eliminated conflicts with other WP scripts by testing with a simple JSFiddle.

EDIT: Interestingly, replacing the <v-table> with a plain HTML <table> works perfectly fine... Any insights on why?

Answer №1

When your template is nested within the regular page, Vue cannot directly access the HTML content. Instead, the browser will attempt to render the page normally, with Vue later using the DOM nodes as a template.

Although browser behavior may vary by vendor, in this scenario they generally function similarly:

  • The browser encounters the unfamiliar <v-table> element and leaves it untouched, assuming another script will handle it.
  • As it comes across table-related elements such as <thead>, <tr>, etc., expecting them to be enclosed within <table> tags (which are missing), these elements are removed along with the v-for directive.
  • Subsequently, Vue swaps out <v-table> for <table> tags, but since there's no content remaining, nothing is displayed.

To resolve this issue, extract the template code from the HTML page and pass it to the component through the template property:

const { createApp, computed, ref, reactive } = Vue;
const { createVuetify } = Vuetify;

const vuetify = createVuetify();
const app = createApp({
  template: `
   <p>table below this point</p>
    <v-table>
      <thead>
        <tr>
          <th class="text-left">URL</th>
          <th class="text-left"></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in desserts" :key="item.name">
          <td className="url">{{ item.name }}</td>
          <td class="title">{{ item.calories }}</td>
        </tr>
      </tbody>
    </v-table>
    <p>table over this point</p>
  `,
  setup() {
    const desserts = ref([{
      name: 'Frozen Yogurt',
      calories: 159,
    }, {
      name: 'Ice cream sandwich',
      calories: 237,
    }, {
      name: 'Eclair',
      calories: 262,
    }, {
      name: 'KitKat',
      calories: 518,
    }, ])

    return {
      desserts
    }
  }
});
app.use(vuetify).mount('#app');
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88e8d9db8cbd6cbd6cc">[email protected]</a>/dist/vue.global.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/3.3.19/vuetify.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5939080918c839ca5d6cbd6cbd4d2">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f1f8f9e3d7a2b9ef">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">


<div id="app"></div>


Alternatively, you can utilize

<table is="vue:v-table">
instead of direct usage of <v-table>:

const { createApp, computed, ref, reactive } = Vue;
const { createVuetify } = Vuetify;

const vuetify = createVuetify();
const app = createApp({
  setup() {
    const desserts = ref([{
      name: 'Frozen Yogurt',
      calories: 159,
    }, {
      name: 'Ice cream sandwich',
      calories: 237,
    }, {
      name: 'Eclair',
      calories: 262,
    }, {
      name: 'KitKat',
      calories: 518,
    }, ])

    return {
      desserts
    }
  }
});
app.use(vuetify).mount('#app');
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdbbb8a88dfee3fee3f9">[email protected]</a>/dist/vue.global.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/3.3.19/vuetify.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f797a6a7b6669764f3c213c213e38">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8ded7d6ccf88d96c0">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">


<div id="app">
  <p>table below this point</p>
    <table is="vue:v-table">
      <thead>
        <tr>
          <th class="text-left">>URL</th>
          <th class="text-left">></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in desserts" :key="item.name">
          <td class="url">{{ item.name }}</td>
          <td class="title">{{ item.calories }}</td>
        </tr>
      </tbody>
    </table>
    <p>table over this point</p>
</div>

This workaround is detailed in the in-DOM Template Parsing Caveats section of the documentation

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

My Angular project is experiencing issues with Socket.IO functionality

After successfully using a post method in my endpoint, I encountered an error when integrating it with socket io. The error pertained to a connection error and method not being found. Any help or source code provided would be greatly ap ...

I'm perplexed as to why my JavaScript code isn't successfully adding data to my database

Recently, I delved into the world of NodeJS and Express. My goal was to utilize Node and Express along with MongoDB to establish a server and APIs for adding data to the database. Specifically, I aimed to write the code in ESmodules syntax for JavaScript. ...

A guide on simulating an emit event while testing a Vue child component using Jest

During my testing of multiple child components, I have encountered a frustrating issue that seems to be poor practice. Each time I trigger an emit in a child component, it prompts me to import the parent component and subsequently set up all other child co ...

Can you guide me on setting a background image URL for rails using javascript?

Within my Rails application, I have a collection of content paired with image buttons (each piece of content has an associated image). When a user clicks on one of these buttons, my goal is to display the corresponding image. All of my images are stored u ...

Unable to designate NODE_ENV as production by utilizing npm and webpack

I'm encountering an issue while trying to access process.env.NODE_ENV within my application. Whenever I check it, I only receive the error message process is not defined. package.json: "scripts": { "dev": "node ./node_mod ...

Maximizing Particle Performance Using JavaScript

I am experimenting with creating particles in JavaScript for the first time, and I'm unsure if the code below is optimized. When I generate 100 particles on the screen, there isn't much of an FPS drop noticeable. However, when multiple clicks o ...

leveraging insertAdjacentHTML within a React Component

I've been working on a project to replicate the Github contribution grid using React. My approach involves using insertAdjacentHTML to fill the grid container with several divs that I can then customize with styles. Unfortunately, I'm encounter ...

CSS animations for loading page content

Currently, I am incorporating animations into my project using HTML5 and CSS3, and the progress has been smooth. I have been able to achieve effects such as: #someDivId { position: absolute; background:rgba(255,0,0,0.75); transition: all 0.7s ...

Sequencing code execution correctly in Node.js

I am currently working on a website that consolidates articles based on user interests. Although I have a backend set up, I am struggling to execute the code in the correct sequence. Essentially, my database consists of MongoDB containing user informatio ...

Hover and hover-off functions in navigation menu

html <div class="hidden-nav"> <h4>lorem</h4> <ul class="decor-menu-list"> <li><a href="#">123</a></li> <li><a href="#">123</a></li> <li><a hre ...

Trouble arises when trying to import Jest with Typescript due to SyntaxError: Import statement cannot be used outside a module

Whenever I execute Jest tests using Typescript, I encounter a SyntaxError while importing an external TS file from a node_modules library: SyntaxError: Cannot use import statement outside a module I'm positive that there is a configuration missing, b ...

Creating a Vue Canvas with Endless Grid Dots and a Dynamic Panning Feature

I'm currently focused on integrating a panning system into the canvas of my Vue application. Although I have successfully implemented the panning system, I am encountering difficulties in efficiently rendering an infinite grid of dots. Below is the c ...

Unable to display data from vuex getters in the story

My MongoDB database contains: user:{ matchesList:[{ kdList:String, winsMatchesList:String }] After creating a Vuex.Store with some getters, I have: matchesList: state => state.matchesList, matchesListKD: state =&g ...

Exploring the functionality of the $.each jQuery iterator. Can someone clarify the distinctions between these two methods?

Having vertices as an array of google.maps.LatLng objects means that they should return latlng points. The first code snippet works perfectly fine, however, I encounter issues when using the second one. // Iterate over the vertices. for (var index =0; ind ...

Learn how to successfully carry on with event.preventdefault in JavaScript

Is there a way to create a modal that prompts the user to confirm if they want to leave the page without committing changes? These changes are not made within a <form> element, but rather on a specific object. I've attempted to use both $route ...

Converting HTML/Javascript codes for Android Application use in Eclipse: A Step-by-Step Guide

How can I implement this in Java? Here is the HTML: <head> <title>Google Maps JavaScript API v3 Example: Geocoding Simple</title> <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="styles ...

Adding Gridster to a WordPress theme

I am having an issue with implementing Gridster into my WordPress plugin. Despite correctly loading the necessary files from the folder, it does not seem to work. function add_my_stylesheet() { wp_enqueue_style( 'myCSS', plugins_url( ' ...

implementing ajax form submission in codeigniter

After submitting my form, validation is checked in the JavaScript file, and then the kickerLogin() function is called. I receive an alert message of datastring. However, the data is not sent to the specified URL in the AJAX request, but the form still ge ...

The Runtime Error encountered in NEXTJS: TypeError - Unable to iterate over 'games' as it is not

Attempting to create my inaugural website that showcases data from an API sans a tutorial. Does it seem like I may have overlooked something? I've successfully fetched the API and confirmed in the console log that the necessary data is present. Howev ...

reCAPTCHA v3 - Alert: There are no existing reCAPTCHA clients available

After coming across a similar issue on Stack Overflow (link to the question here), I attempted to implement reCAPTCHA on my website to combat spam emails received through the form. Despite following Google's instructions, I encountered an error that p ...