javascript - 如何在vue组件中导入js文件?

标签 javascript json 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>

最佳答案

Toplist.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>

针对您的案例的示例答案。

关于javascript - 如何在vue组件中导入js文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724194/

相关文章:

javascript - 使用 Intl.NumberFormat 有条件地删除格式化货币值上的小数

javascript - jQuery - 将类从子元素添加到父元素

android - 我应该如何将 json 放在 GET 请求中?

mysql - 如何使用 Laravel、MySql、JSON 按不同城市对订单进行分组

javascript - 未从工厂获取 JSON 数据

javascript - 检查jquery `success`方法参数问题

json - Nginx返回带有伪造的200状态代码的空json对象

javascript - 为Vue-Multipane组件添加分页功能

vue.js - 更改数据的值,但数据没有更新

vue.js - 模块解析失败 : Unexpected character '�' (1:0)