Search code examples
vue.jsvue-routerjson-server

The json data can't into Vue router


Below is the JSON data that I used for practice API call. But when I try to load the JSON data, I couldn't able to do it.

I don't know why I use data(){.....

the data.json can't show in web, but in console, data.json has be read like this img:

https://i.sstatic.net/jWDrh.png


my data.json is:

{
  "contents": [
    {
      "content": "456",
      "id": 2
    }
  ]
}

//This is my Vue.js

let List={
      template:
      `<div>
        <p>
          <input type="text" v-model.trim="input">
          <a href="javascript:;" v-on:click="createHandler">Create</a>
        </p>
      <ol>
         <li v-for="(item,index) in contents" :key="item.id">
            {{ item.content }}
        </li>
      </ol>
      </div>`,
      data() {
        return {
          input: '',
          contents:[]
        };
      }
    let router= new VueRouter({
        routes:[
          {
            path:'/',
            name:'list',
            component:List
          },
          {
            path:'/update/:id',
            name:'update',
            component:Edit
          },
          {
            path:'*',
            redirect:'/'
          }
        ]

    })
      new Vue({
        el: "#app",
        router:router,
        mounted() {
          axios.get('http://localhost:3000/contents').then((res) => {
            console.log(res.data);
            this.contents = res.data;
          })
        }
      })
<!-- This is my HTML -->

<div id="app">
      <router-view></router-view>
    </div>
   


Solution

  • here is a Jsfiddle

    first you need to declare a vue component .... because the one you have let list = {...} is not a component and the router doesn't recognize it , also your vue instance data is not your component data ... you need to pass the array as a prop .... here is how you do it :

    var list = Vue.component('list',{
          template:
          `<div>
            <p>
              <input type="text" v-model.trim="input">
              <a href="javascript:;" v-on:click="createHandler">Create</a>
            </p>
          <ol>
             <li v-for="(item,index) in contents" :key="item.id">
                {{ item.content }}
            </li>
          </ol>
          </div>`,
          props : ['contents'],
          data() {
            return {
              input: '',
            }
           }
          })
    
      let router= new VueRouter({
        routes:[
          {
            path:'/',
            name:'list',
            component:list
          },
          {
            path:'/update/:id',
            name:'update',
            component:Edit
          },
          {
            path:'*',
            redirect:'/'
          }
        ]
      })
    
       new Vue({
        el: "#app",
        router:router,
        data(){
          return {
            contents : []
          }
        },
        mounted() {
          axios.get('http://localhost:3000/contents').then((res) => {
           this.contents = res.data
          })
        }
      })
    <div id="app">
          <router-view :contents = "contents"></router-view>
        </div>