【发布时间】:2018-06-23 11:44:50
【问题描述】:
我使用 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
}
]
})
机会.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 未加载,因此具有交换名称的表行未显示在浏览器中。
如何正确地将数据加载到视图中并打印表格?
谢谢
【问题讨论】:
-
exchanges是根级数据属性。您必须将其作为属性传递给Opportunities.vue组件。 -
像这样:codesandbox.io/s/jjrw9rjy93。或者,使用像 Vuex 这样的状态管理系统。
-
有趣的Vuex版本codesandbox.io/s/oqvmp4rmz6
-
这是一个解释传递参数到组件stackoverflow.com/a/34541828/6805529的答案
标签: javascript vue.js