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

JavaScript code that triggers a function when the mouse is moved over a description box on a

As a beginner in the field of Computer Science, I recently embarked on a project to create a website for sharing science articles with students. The goal is to spark curiosity and interest in science through this platform. One key feature I wanted to inclu ...

Dynamically loading a webpage with an element roster

I have a list and I want it so that when a user clicks on the sport1 list item, results from the database table sport1 will be displayed. Similarly, if they click on the education1 list item, results from the education1 table should be shown. The issue i ...

Troubleshooting Issue with Angular Property Binding for Image Dimensions

Currently, I am exploring property binding in Angular through an example. The idea is to use an image and bind its width, height, and src attributes using property binding. Surprisingly, even though there are no errors and the image renders successfully vi ...

Developing a custom library to enable Ajax capabilities in web applications

Currently, I am in the process of developing my own personal library. jQuery doesn't quite meet my needs, and I prefer having a clear understanding of what is happening within my code. Nevertheless, I'm encountering an issue with the ajax functio ...

showing the values stored in local storage

My goal is to show only the values stored in local storage, excluding the key value that displays all data after submitting the login form. welcome <span id="demo"></span>; <script> document.getElementById('demo').innerHTML = ...

Issue with unit testing a ViewportRuler in Angular 2 Material Library

I am currently working on an Angular2 component that includes a tab control from @angular/material. During testing of my component (refer to the simplified code below), I encountered the following error: Error: Error in ./MdTabHeader class MdTabHeader - ...

What is the best way to execute operations on two distinct datasets within a single function using RxJS?

Is there a way to perform operations on two separate data collections within a single function in RxJS, returning an observable instead of void? This function is located within a service and I intend to subscribe to it from my component. I believe some re ...

Having trouble with Vue component registration repeatedly failing

Currently, I am working on a front-end project using [THIS VUE TEMPLATE][https://www.creative-tim.com/product/vue-material-dashboard-pro] The issue I am facing involves trying to register a component locally and encountering the following error: "1 ...

Having trouble making the swing effect trigger on mouseover but not on page load

I am trying to achieve a swinging effect on mouseover only, not on page load. Below is the JS code I have: (function swing() { var ang = 20, dAng = 10, ddAng = .5, dir = 1, box = document.getElementById("box"); (function setAng(ang){ ...

Separate configurations in vue.config.js for running npm serve and npm build scripts

When it comes to customizing the webpack configuration, I am utilizing vue.config.js: const BundleTracker = require("webpack-bundle-tracker"); module.exports = { publicPath: 'http://0.0.0.0:8080', outputDir: './dist/', chainWeb ...

Tips for Navigating and Scrolling to an Element by its ID in a Next.js Page Enhanced with AnimatePresence

Currently, I am utilizing Framer Motion to add animations to the page transitions in a Next.js project. However, when implementing AnimatePresence, it seems to interfere with the navigation to specific elements using hash links (id). The seamless transitio ...

HTML control for adjusting the pan and tilt of a device

I'm looking to develop an interactive input where users can drag a dot within a box and see the 2D coordinates of that dot displayed. The goal is to use this feature to control pan and tilt values. A similar functionality from another application is s ...

MUI Error: Incorrect prop provided to menu item

I am encountering an issue with a React component that generates a list of elements, each containing a button to open a menu. The problem is that the props being passed to each menu are incorrect; they always reflect the props of the last element in the ...

Weekly downloads for NPM show no activity

https://i.stack.imgur.com/4Uhk4.png https://i.stack.imgur.com/0vikS.png Why are all the weekly downloads showing zero for npm packages? I'm new here and confused about why this is happening, any insights? If you could please help me open this issue ...

Is there a way to determine if a button was clicked using twig?

I need assistance with implementing a button in my twig file within a table that will remove an element from an array. Ideally, I would like to remove the element based on its index. From the research I have conducted, it seems that data manipulation shou ...

Encountering a npm error E404 when trying to install unicons package for React development

I recently started working on a weather app project using create-react-app and encountered an issue while trying to install unicons for the project. Despite attempting a few solutions, I was unable to resolve the problem. Here is the command I used for th ...

Incorporate Stripe Elements into your Nuxt Js application

I recently managed to integrate Stripe into my React + Spring Boot application by following the guidelines provided in this documentation: https://stripe.com/docs/stripe-js/react. I used it in my React class component. Now, I am transitioning to Nuxt from ...

What is the cause behind the failure of this regular expression in JavaScript?

I have been struggling to make this code display "matched!" for the specific string. I've tried various methods but nothing seems to be working. Here's the HTML part: <div id="ssn">123-45-6789</div> And here's the JavaScript p ...

How can I dynamically populate my select field with JSON data using React.js?

My goal is to fetch data from an API and use the 'symbol' JSON field as options for a select dropdown. However, I'm encountering an issue: 'TypeError: Cannot read property 'length' of undefined.' Beneath my code, I' ...

The Clash of Form Action and JavaScript

Can someone help me with this issue? I have a form and a script to trigger an alert message. The code I'm using is working well: <input id="autocomplete" class="input_message" type="text" name="message" autocomplete="off" placeholder="...typ ...