1
votes

I am facing an issue in my Vue project while trying to render the data from local JSON. I have read instructions and followed them-- every step-- yet the error prevails. Since I am new to Vue and advanced programming itself, I seek help.

english.json

{
  "0": {
    "text": "This be a square",
    "color": "blue",
    "size": "small"
  },
  "1": {
    "text": "here lies a square",
    "color": "red",
    "size": "medium"
  },
  "2": {
    "text": "Tharr be a squaree",
    "color": "black",
    "size": "large"
  }
}

ExperienceText.vue

<template>
  <article class="content__box" :v-for="data in expJson">
    {{data.text}}
    <h6>{{data.text}}</h6>
    <h3>{{data.color}}</h3>
    <p>{{data.size}}</p>
  </article>
</template>
<script>
import json from "@/components/json/english.json";
export default {
  name: "ExperienceText",
  data() {
    return {
      expJson: json
    };
  }
};
</script>

Error

[Vue warn]: Property or method "data" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <ExperienceText> at src/components/mainComp/subComp/ExperienceText.vue
       <ExperienceSection> at src/components/mainComp/ExperienceSection.vue
         <Experience> at src/components/mainComp/Experience.vue
           <Main> at src/components/Main.vue
             <Container> at src/views/Container.vue
               <App> at src/App.vue
                 <Root>

Show 60 more frames
vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "data" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <ExperienceText> at src/components/mainComp/subComp/ExperienceText.vue
       <ExperienceSection> at src/components/mainComp/ExperienceSection.vue
         <Experience> at src/components/mainComp/Experience.vue
           <Main> at src/components/Main.vue
             <Container> at src/views/Container.vue
               <App> at src/App.vue
                 <Root>

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: Cannot read property 'text' of undefined"

found in

---> <ExperienceText> at src/components/mainComp/subComp/ExperienceText.vue
       <ExperienceSection> at src/components/mainComp/ExperienceSection.vue
         <Experience> at src/components/mainComp/Experience.vue
           <Main> at src/components/Main.vue
             <Container> at src/views/Container.vue
               <App> at src/App.vue
                 <Root>

vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'text' of undefined
    at Proxy.render (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules/.cache/vue-loader","cacheIdentifier":"db619a62-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/mainComp/subComp/ExperienceText.vue?vue&type=template&id=088f3b1e& (app.js:1089), <anonymous>:15:36)
    at VueComponent.Vue._render (vue.runtime.esm.js?2b0e:3548)
    at VueComponent.updateComponent (vue.runtime.esm.js?2b0e:4066)
    at Watcher.get (vue.runtime.esm.js?2b0e:4479)
    at new Watcher (vue.runtime.esm.js?2b0e:4468)
    at mountComponent (vue.runtime.esm.js?2b0e:4073)
    at VueComponent.Vue.$mount (vue.runtime.esm.js?2b0e:8415)
    at init (vue.runtime.esm.js?2b0e:3118)
    at createComponent (vue.runtime.esm.js?2b0e:5978)
    at createElm (vue.runtime.esm.js?2b0e:5925)

The JSON arrived

Proof of json data reached.

In my app, the article tag is not visible at all. I am pretty sure it is because the JSON data is not being rendered properly. How do i fix this issue?

2
Remove the : in front of v-forDan
2:3 error The template root disallows 'v-for' directives | I was returned with this issue. And, hence the compile failed.Ajaya Bajracharya
I read the link article it seems ok, not to have : before v-for, But when I do it in my code I get following issues 2:3 error The template root disallows 'v-for' directives vue/valid-template-root 2:3 error Elements in iteration expect to have 'v-bind:key' directives vue/require-v-for-keyAjaya Bajracharya
` 2:32 error Parsing error: missing-whitespace-between-attributes vue/no-parsing-error 2:32 error Parsing error: unexpected-character-in-attribute-name vue/no-parsing-error 7:4 warning Parsing error: Unexpected closing tag "article". It may happen when the tag has already been closed by another tag. For more info see w3.org/TR/html5/… prettier/prettier ` ✖ 5 problems (4 errors, 1 warning)Ajaya Bajracharya

2 Answers

2
votes

First, remove the : from v-for, since it's a directive and not a binding.

<article class="content__box" v-for="data in expJson">

You also want a :key on all v-for elements. Since your data doesn't have a unique identifier, we can use the index:

<article class="content__box" v-for="(data, index) in expJson" :key="index">

Additionally, this still gives an error because all components must have one root element and v-for can create potentially multiple root elements. You can wrap everything in a div:

<template>
  <div>
    <article class="content__box" v-for="(data, index) in expJson" :key="index">
      {{data.text}}
      <h6>{{data.text}}</h6>
      <h3>{{data.color}}</h3>
      <p>{{data.size}}</p>
    </article>
  </div>
</template>
0
votes

Hope this helps.

remove : colon from :v-for

const yourJson = {
  "0": {
    "text": "This be a square",
    "color": "blue",
    "size": "small"
  },
  "1": {
    "text": "here lies a square",
    "color": "red",
    "size": "medium"
  },
  "2": {
    "text": "Tharr be a squaree",
    "color": "black",
    "size": "large"
  }
}

new Vue({
  el: '#app',
  data() {
    return {
      expJson: yourJson,
    };
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <article class="content__box" v-for="data in expJson" :style="{backgroundColor: data.color}" style="color: white; padding: 10px">
    {{data.text}}
    <h6>{{data.text}}</h6>
    <h3>{{data.color}}</h3>
    <p>{{data.size}}</p>
  </article>
</div>