简体   繁体   English

在Vuejs中使用静态JSON进行动态路由

[英]Dynamic routing with static JSON in Vuejs

I have a static local JSON file which includes my content. 我有一个包含我的内容的静态本地JSON文件。 I am using VueResources and VueRouter . 我正在使用VueResourcesVueRouter My aim is, making a list component for displaying items in local JSON file. 我的目标是制作一个列表组件,用于在本地JSON文件中显示项目。 Then, if user clicks of an item, showing this item's content in another page. 然后,如果用户单击某个项目,则在另一个页面中显示该项目的内容。 For this, I used $route.params . 为此,我使用了$route.params

Here is my list component. 这是我的清单组件。 I called it Objects.vue 我称它为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 我称它为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 基本上是我的json文件

{
  "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 route / index.js文件

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 . 但是问题是,当我单击一个项目时, id返回undefined For this reason try to show http://localhost:8080/ undefined 因此,请尝试显示http:// localhost:8080 / undefined

How can I handle this? 我该如何处理? What am I missing? 我想念什么?

this.lists.id is always undefined. this.lists.id始终是未定义的。 Our this.lists is an array. 我们的this.lists是一个数组。 You should place router inside v-for and access item.id 您应该将路由器放置在v-for内并访问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 作为讨论,您可以更新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 或者如果您可以导入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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM