简体   繁体   English

Vuejs 和 webpack 加载程序 - 无法在数据表上加载数据

[英]Vuejs and webpack loader - can't load data on a datatable

I'm using Webpack-Loader to load VueJS in my Django project.我正在使用 Webpack-Loader 在我的 Django 项目中加载 VueJS。 I'm very new to Vue, and i'm trying to create a simble component that fetches data from the backend using an API call and shows this data on Vuetify datatable.我是 Vue 的新手,我正在尝试创建一个简单的组件,它使用 API 调用从后端获取数据并在 Vuetify 数据表上显示此数据。

The issue with my code is that while the request is executed, nothing is shown on the datatable, so the table shows but nothing is inside of it.我的代码的问题是,在执行请求时,数据表上没有显示任何内容,因此表格显示但其中没有任何内容。

I'm having an hard time understanding if there is a problem with the code or if the problem is with my config or my imports.我很难理解代码是否有问题,或者问题是否出在我的配置或我的导入上。

Here is the component where i'm performing the request and show the data on the datatable:这是我执行请求并在数据表上显示数据的组件:

App.vue App.vue

<template>
  <v-simple-table dark>
    <template v-slot:default>
      <thead>
        <tr>
          <th class="text-left">
            Asset
          </th>
          <th class="text-left">
            Total
          </th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="item in balances"
          :key="item.asset">
          <td>{{ item.asset }}</td>
          <td>{{ item.total }}</td>
        </tr>
      </tbody>
    </template>
  </v-simple-table>
</template>


<script>

export default {
  data() {
    return {
      balances: [],
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      fetch('http://127.0.0.1:8000/binance/getbalance')
        .then(response => response.json())
        .then(data => {
          this.balances = data
          console.log(data)
        })
    }
  }
}

</script>

main.js主程序

import Vue from "vue/dist/vue.js";
import Vuex from "vuex";
import storePlugin from "./vuex/vuex_store_as_plugin";
import App from './App.vue'
import Vuetify from "vuetify";
import "vuetify/dist/vuetify.min.css";

Vue.use(Vuetify);
Vue.use(Vuex);
Vue.use(storePlugin);
Vue.config.productionTip = false;
  
new Vue({
    el: '#app',
    render: h => h(App),
})

Here is my vue.config.js这是我的 vue.config.js

const BundleTracker = require("webpack-bundle-tracker");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;


const pages = {
    'main': {
        entry: './src/main.js',
        chunks: ['chunk-vendors']
    },

}

module.exports = {
    pages: pages,
    filenameHashing: false,
    productionSourceMap: false,
    publicPath: process.env.NODE_ENV === 'production'
        ? 'static/vue'
        : 'http://localhost:8080/',
    outputDir: '../django_vue_mpa/static/vue/',

    chainWebpack: config => {

        config.optimization
            .splitChunks({
                cacheGroups: {
                    moment: {
                        test: /[\\/]node_modules[\\/]moment/,
                        name: "chunk-moment",
                        chunks: "all",
                        priority: 5
                    },
                    vendor: {
                        test: /[\\/]node_modules[\\/]/,
                        name: "chunk-vendors",
                        chunks: "all",
                        priority: 1
                    },
                },
            });

        Object.keys(pages).forEach(page => {
            config.plugins.delete(`html-${page}`);
            config.plugins.delete(`preload-${page}`);
            config.plugins.delete(`prefetch-${page}`);
        })

        config
            .plugin('BundleTracker')
            .use(BundleTracker, [{filename: '../vue_frontend/webpack-stats.json'}]);

        // Uncomment below to analyze bundle sizes
        // config.plugin("BundleAnalyzerPlugin").use(BundleAnalyzerPlugin);
        
        config.resolve.alias
            .set('__STATIC__', 'static')

        config.devServer
            .public('http://localhost:8080')
            .host('localhost')
            .port(8080)
            .hotOnly(true)
            .watchOptions({poll: 1000})
            .https(false)
            .headers({"Access-Control-Allow-Origin": ["*"]})

    }
};

Versions used:使用的版本:

Vue 2, vuetify@2.3.21

The data is a very simple JSON array of less than 20-30 elements, it looks like this:数据是一个非常简单的 JSON 数组,少于 20-30 个元素,它看起来像这样:

[{"asset": "XYZ", "total": 123}, {"asset": "XYZ2", "total": 456"} ... ]

The request is being executed, since console.log() will print the data, the table won't show any data, it's just empty.正在执行请求,因为 console.log() 将打印数据,表格不会显示任何数据,它只是空的。

Here are the only errors i found in my console:这是我在控制台中发现的唯一错误:

vuetify.js?ce5b:42906 [Vuetify] Multiple instances of Vue detected

[Vue warn]: $attrs is readonly.

found in

---> <VSimpleTable>
       <App> at src/App.vue
         <Root>

[Vue warn]: $listeners is readonly.

found in

---> <VSimpleTable>
       <App> at src/App.vue
         <Root>

EDIT This isn't going to append data to the table, which means that the problem is most likely not my data:编辑这不会将 append 数据添加到表中,这意味着问题很可能不是我的数据:

.then(data => {
          this.balances = [{"asset": 1, "total": "1"}, {"asset": 2, "total": 2}, {"asset": 3, "total": 3}]

        })

Vuetify didn't have simple-table in 1.xx versions. Vuetify 在 1.xx 版本中没有simple-table Instead they used v-data.table instead it seems.相反,他们似乎使用了v-data.table

I've made a codesandbox to test this:我做了一个codesandbox来测试这个:

https://codesandbox.io/s/vuetify-data.table-forked-4pxqb?file=/src/components/AppTable.vue https://codesandbox.io/s/vuetify-data.table-forked-4pxqb?file=/src/components/AppTable.vue

Vuetify 2.3.xx验证 2.3.xx 验证 2.xx

Vuetify 1.4.xx验证 1.4.xx 在此处输入图像描述

It looks like i was importing in the wrong way.看起来我以错误的方式导入。 Changing my imports to:将我的导入更改为:

import Vue from "vue";
import App from "./App.vue";
import Vuetify from "vuetify";
import "vuetify/dist/vuetify.min.css";

fixed the problem entirely.完全解决了这个问题。 I still don't understand what changes, so any kind of explanation is very welcome!我仍然不明白发生了什么变化,所以非常欢迎任何解释!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM