Present table of data, acquired from an endpoint, with option to sort by different values I have built a Vue app which fetches data from an endpoint and presents that data in a table with sortable columns.

The app is based on this React challenge which is to create a table of users and their properties, from the data provided in the endpoint.

Here are the user stories fulfilled:


  
  I can see a table of the Free Code Camp campers who've earned the most brownie points in the past 30 days.
  I can see how many brownie points they've earned in the past 30 days, and how many they've earned total.
  I can toggle between sorting the list by how many brownie points they've earned in the past 30 days and by how many brownie points
  they've earned total.
  


Here is my code (working example):



const endpoint = 'https://fcctop100.herokuapp.com/api/fccusers/top/recent'

const store = {
  state: {
    sortingBy: ''
  }
}

new Vue({
  el: '#app',
  data() {
    return {
      data: [],
      state: store.state
    }
  },
  mounted() {
    Vue.http.get(endpoint).then(response =&gt; {
      this.data = response.body
    }, response =&gt; {
      console.warn('There was a data error:', response)
    })
  },
  methods: {
    sortByRecent() {
      this.data = _.sortBy(this.data, 'recent').reverse()
      store.state.sortingBy = 'recentDescending'
    },
    sortByAllTime() {
      this.data = _.sortBy(this.data, 'alltime').reverse()
      store.state.sortingBy = 'allTimeDescending'
    },
    getActiveClass(string) {
      return {active: store.state.sortingBy.includes(string)}
    }
  }
})
#app {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.active {
  font-weight: bold;
}
button {
  font-size: 20px;
}
img {
  border-radius: 50%;
  height: 30px;
  width: 30px;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"&gt;&lt;/script&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.0.3/vue-resource.min.js"&gt;&lt;/script&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"&gt;&lt;/script&gt;

&lt;div id="app"&gt;
  &lt;button @click="sortByRecent"&gt;Sort by Recent Points&lt;/button&gt;
  &lt;button @click="sortByAllTime"&gt;Sort by All Time Points&lt;/button&gt;
  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;td&gt;#&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;Username&lt;/td&gt;
        &lt;td :class="getActiveClass('recent')"&gt;Recent Points&lt;/td&gt;
        &lt;td :class="getActiveClass('allTime')"&gt;Total Points&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tr v-for="(user, index) in data"&gt;
      &lt;td&gt;{{index + 1}}&lt;/td&gt;
      &lt;td&gt;
        &lt;img :src="user.img"&gt;
      &lt;/td&gt;
      &lt;td&gt;{{user.username}}&lt;/td&gt;
      &lt;td&gt;{{user.recent}}&lt;/td&gt;
      &lt;td&gt;{{user.alltime}}&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/div&gt;




Here is a repo with all the code (bootstrapped with vue-loader webpack).

My questions are:


I have placed a method for computing a CSS classname in methods - getActiveClass. In the documentation, it specifies that such methods can live inside the computed object:


  We can also bind to a computed property that returns an object.


However when doing so, I get the following console error in the browser:


  Uncaught TypeError: _vm.getActiveClass is not a function


Why does placing this method inside a computed object render this error?
My project was bootstrapped with vue-loader's webpack preset. In order to make the API call to my endpoint, I had to import Vue so that I could use vue-resource. Should the API call be made in main.js and then passed in as a prop to App.vue to avoid importing Vue in multiple places?
The data is received from the API call in any of the lifecycle methods beforeCreate, created, beforeMount and mounted. Which is the correct one for it to go?
Does it make sense for this code to all live in one single file component, or should it be split up into separate smaller components?
I'm new to Vue myself so this may not be 100% accurate, just my own opinions. Kudos for learning and reaching out for feedback! You seem to have grokked a lot so I'm impressed.


Computed properties are used the same as data properties. You do not call them like methods, and you can't pass parameters to computed properties. Instead of :class="getActiveClass('recent')" you'd have to use something like :class="{active: isSortedByRecentPoints}" or :class="recentPointsHeaderClasses". But what you have works just fine IMO.
There is no penalty to importing Vue in multiple places, and it is actually good practice. Think of it like Java imports, you are declaring all of our external dependencies in your header so you know what kinds of operations this code is likely to perform without reading every single line.
The correct hook is determined by your use case. For this example any one of the hooks mentioned would work fine. For more general advise, I tend to prefer the mounted hook, but that's just me and I don't know what I don't know. Will the component get mounted multiple times? Will you have all of the data you need to make the call at created? I'm not familiar enough with the details of Vue to give more in depth advise.
For this example one Vue instance works just fine. If you want to experiment you can extract a true component from your code and create an API: The table data would be an input prop, and any actions on that data would be output events. I wouldn't consider sorting to be a reasonable output event. How about a view details event on the user?


If you're looking for extra credit I'd recommend looking into Vuex for your store. Currently you're mutating the store directly, which kind of goes against the whole point of using a store. Also a store is overkill for this example, but I'd recommend it for learning purposes. I'll give you a hint: Instead of using state in your data object you will probably want to bind directly to sortingBy and then use $store.commit to alter it in your methods. Commit will trigger a mutation to change the state, which will in turn update your component's data. You can do something similar for the table data - put it in a store and dispatch a fetchdata action to GET your data, then commit it to the state. This may seem like overkill at first (and it probably is for simple apps) until you start playing around with the time travelling debugger in the Vue devtools, hot module replacement, and the like. Super cool stuff.