Search code examples
javascriptvue.jsvuejs2vue-routervue-resource

Dynamic routing with static JSON in Vuejs


I have a static local JSON file which includes my content. I am using VueResources and VueRouter. My aim is, making a list component for displaying items in local JSON file. Then, if user clicks of an item, showing this item's content in another page. For this, I used $route.params.

Here is my list component. I called it Objects.vue

<template>
  <div id="objects">
    <router-link :to="'/' + this.lists.id"><div v-for="(item, index) in lists" class="list">{{ item.title }} {{ item.id }}</div></router-link>
  </div>
</template>

<script>
//import json from './../assets/data.json'
export default {
  name: 'Objects',
  data() {
    return {
      lists: [],
    };
  },
  methods: {
    getObjects() {
       this.$http.get('/static/data.json')
          .then((response) => {
            console.log(response.body);
            this.lists = response.body.objects;
          })
    }
  },
  mounted() {
    this.getObjects();
    console.log(this.lists.id);
  }
};
</script>

<style scoped>

</style>

And here it is my item component. I called it Object.vue

<template>
  <div id="object">
    <div>
      {{ object.id }}
    </div>
    <div>
      {{ object.title }}
    </div>
  </div>
</template>

<script>
import json from './../assets/data.json'
export default {
  name: 'Object',
  data() {
    return {
      id: this.$route.params.id,
      object: {},
    };
  },
  methods: {
    getObjects() {
       this.$http.get('/static/data.json/' + this.id)
          .then((response) => {
            console.log(response);
            this.object = response.body.objects;
          })
    }
  },
  mounted() {
    this.getObjects();
  }
};
</script>

And basicly my json file

{
  "objects": [
    {
      "id": 0,
      "title": "a"
    },
    {
      "id": 1,
      "title": "b"
    },
    {
      "id": 2,
      "title": "c"
    },
    {
      "id": 3,
      "title": "d"
    },
    {
      "id": 4,
      "title": "e"
    },
    {
      "id": 5,
      "title": "f"
    },
    {
      "id": 6,
      "title": "g"
    },
    {
      "id": 7,
      "title": "h"
    },
    {
      "id": 8,
      "title": "i"
    },
    {
      "id": 9,
      "title": "j"
    }
  ]
}

route/index.js file

import Vue from 'vue';
import Router from 'vue-router';
import Visit from '@/components/Visit';
import Objects from '@/components/Objects';
import Community from '@/components/Community';
import Instagram from '@/components/Instagram';
import Object from '@/components/Object';

Vue.use(Router);
export default new Router({
  routes: [
    {
      path: '/',
      name: 'Objects',
      component: Objects,
    },
    {
      path: '/Visit',
      name: 'Visit',
      component: Visit,
    },
    {
      path: '/Community',
      name: 'Community',
      component: Community,
    },
    {
      path: '/Instagram',
      name: 'Instagram',
      component: Instagram,
    },
    {
      path: '/:id',
      name: 'Object',
      component: Object,
    },
  ],
});

List component works fine and show every items. But problem is, when I clicked an item, id is returned undefined. For this reason try to show http://localhost:8080/undefined

How can I handle this? What am I missing?


Solution

  • this.lists.id is always undefined. Our this.lists is an array. You should place router inside v-for and access item.id

    Should be

    <div v-for="(item, index) in lists" class="list" :key="item.id">
      <router-link :to="'/' + item.id">
       {{ item.title }} {{ item.id }}
      </router-link>
    </div>
    

    Updated

    As discussion, you can update your Object.vue

    <template>
      <div id="object">
        <div>
          {{ object.id }}
        </div>
        <div>
          {{ object.title }}
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: 'Object',
      data() {
        return {
          id: this.$route.params.id,
          object: {},
        };
      },
      methods: {
        getObjects() {
           this.$http.get('/static/data.json')
              .then((response) => {
                console.log(response);
                this.object = response.body.objects.find(item => item.id == this.id)
              })
        }
      },
      mounted() {
        this.getObjects();
      }
    };
    </script>
    

    or if you can import data.json

    <template>
      <div id="object">
        <div>
          {{ object.id }}
        </div>
        <div>
          {{ object.title }}
        </div>
      </div>
    </template>
    
    <script>
    import json from './../assets/data.json'
    export default {
      name: 'Object',
      data() {
        return {
          id: this.$route.params.id,
          object: {},
        };
      },
      mounted() {
        this.object = json.objects.find(item => item.id == this.$route.params.id)
      }
    };
    </script>