简体   繁体   English

如何在Vue.js应用程序中加载数据

[英]How to load data in Vue.js app

I used the vue cli webpack to generate an app. 我使用vue cli Webpack生成了一个应用程序。 Now I'm trying to load some data into a view without success. 现在,我试图将某些数据加载到视图中而没有成功。 Here is code in current state: 这是当前状态的代码:

main.js main.js

 // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import BootstrapVue from 'bootstrap-vue' import 'bootstrap/dist/css/bootstrap.css' import 'bootstrap-vue/dist/bootstrap-vue.css' Vue.config.productionTip = false Vue.use(BootstrapVue) /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>', data: { exchanges: [ {name: 'gdax', price: 1450}, {name: 'bitfinex', price: 1525} ] } }) 

App.vue 应用程序

 <template> <div id="app" class="container"> <h1>Arb Bot</h1> <router-view/> </div> </template> <script> export default { name: 'App' } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; color: #2c3e50; margin-top: 60px; } </style> 

routes/index.js 路线/index.js

 import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import Opportunities from '@/components/Opportunities' Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld }, { path: '/opportunities', name: 'Opportunities', component: Opportunities } ] }) 

Opportunities.vue 机会

 <template> <div> <h2>Bitcoin prices</h2> <table> <tr> <td>Exchange</td><td>Price</td> </tr> <tr v-for="exchange in exchanges" :key="exchange.name"> <td>{{ exchange.name }}</td><td>{{ exchange.price }}</td> </tr> </table> </div> </template> 

The view is rendering fine except the data isn't loading so the table rows with the exchange names aren't showing up in the browser. 该视图呈现良好的外观,只是未加载data因此带有交换名称的表行未显示在浏览器中。

How can I properly load the data into the view and print the table? 如何将数据正确加载到视图中并打印表格?

Thanks 谢谢

el: '#app',

refers to the element with ID=app, which in your case is missing. 指代ID = app的元素,在您的情况下该元素缺失。

You can fix it by giving the table the id=app 您可以通过为表赋予id = app来修复它

<table id="app">

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

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