javascript - 如何在vue组件中导入js文件?
问题描述
我正在尝试将结果(json)导入 vue 组件但不工作?
结果:
[{"id":"d023c5e3-ca3c-4d97-933a-1112a8516eee",
"score":9001,
"updated":"2018-12-07T13:48:33.6366278",
"player":Johanna,
"category":Funny},
{"id":"398b65fb-e741-4801-be49-926b111ec871",
"score":99,
"updated":"2018-12-11T11:13:42.8312936",
"player":Johanna,
"category":Music}]
在 GetResult.js 中
import axios from 'axios'
const url = 'http://localhost:5000/api/Results';
export default {
data () {
return {
results: {}
}
},
created () {
axios.get(url)
.then(response => {
console.log(response.data)
this.$data.results = response.data
})
.catch(err => {
console.log(err)
})
}
}
在 Toplist.vue
<template>
<div class="TopList">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Score</th>
<th>category</th>
</tr>
</thead>
<tbody>
<tr v-for="result in resultList" :key="result.id">
<td>{{ result.player }}</td>
<td>{{ result.score }}</td>
<td>{{ result.category }}</td>
</tr>
</tbody>
</table>
</div>
<script>
import results from './ResultApi/GetResult.js'
export default {
name: 'TopList',
data() {
return {
resultList: results
}
}
}
</script>
解决方案
顶级列表.vue
// Ignoring the HTML part
<script>
export default {
name: 'TopList',
data() {
return {
results: []
}
},
mounted () {
this.getResults()
},
methods: {
getResults () {
axios.get(url)
.then(response => this.results = response.data)
.catch(err => console.error(err))
}
}
}
</script>
您的案例的示例答案。
推荐阅读
- batch-file - 完美地批量转发参数
- typescript - HTML 文件中的 TypeScript 智能感知
- php - 如何为数组中的每个元素创建一个变量
- c# - Xamarin.forms ScanOverlay 如何将最后一个扫描项显示到标签或 XAML 中的类似内容
- scala - 从 Scala 案例对象中使用超类的 require 子句
- android - 手机关机时如何在后台自动杀死应用程序?
- java - 静态成员的方法是否被认为是静态的?
- kubernetes - 使用 Kubernetes 注册 GOCD 代理
- ios - 检索 RLMArray 中存在值的 RLMObject
在 iOS 中 - c# - 如何在c#或jquery中将双引号替换为单引号?