我使用vue cli webpack生成一个应用程序。现在我正在尝试将一些数据加载到视图中而没有成功。这是当前状态的代码:
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>
路由/ 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>
视图正常呈现,但data
未加载,因此具有交换名称的表行未显示在浏览器中。
如何将数据正确加载到视图中并打印表格?
由于
答案 0 :(得分:-2)
el: '#app',
指ID = app的元素,在您的情况下缺少。
你可以通过给表id = app
来修复它<table id="app">