1

I am trying to render a component but I get the error: property or method "joke" is not defined in is not defined on the instance but referenced during render. I am using the dad jokes api to get data via the axios http library. Here is my code:

var joke = Vue.component('joke', {
  template: '#joke',
  data() {
    return {
      jokes: []
    };
  },
  created() {
    axios
      .get('https://icanhazdadjoke.com/search', {
        headers: {
          Accept: 'application/json'
        },
        params: {
          limit: 30
        }
      })
      .then(response => {
        this.jokes = response.data.results;
      });
  }
});

new Vue({
  el: '#main'
});
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>vue Dad JOkes</title>
    <!--styles-->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
    <!--scripts-->
    <script src="https://unpkg.com/[email protected]/dist/vue.js" charset="utf-8"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  </head>
  <body>
    <div id="main">
      <joke></joke>
    </div>
    <template id="joke">
      <ul>
        <li v-for="joke in jokes"></li>
        <p>{{joke.joke}}</p>
      </ul>

    </template>


    <script src = "app.js" charset="utf-8"></script>
  </body>
 
  </html>

1 Answer 1

1

It's a simple issue with the html, you had ended the </li> before using {{joke}}

Change

<ul>
  <li v-for="joke in jokes"></li>
  <p>{{joke.joke}}</p>
</ul>

to

<ul>
  <li v-for="joke in jokes">
    <p>{{joke.joke}}</p>
  </li>
</ul>

Here's your working example:

var joke = Vue.component('joke', {
  template: '#joke',
  data() {
    return {
      jokes: []
    };
  },
  created() {
    axios
      .get('https://icanhazdadjoke.com/search', {
        headers: {
          Accept: 'application/json'
        },
        params: {
          limit: 30
        }
      })
      .then(response => {
        this.jokes = response.data.results;
      });
  }
});

new Vue({
  el: '#main'
});
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>vue Dad JOkes</title>
    <!--styles-->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
    <!--scripts-->
    <script src="https://unpkg.com/[email protected]/dist/vue.js" charset="utf-8"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  </head>
  <body>
    <div id="main">
      <joke></joke>
    </div>
    <template id="joke">
      <ul>
        <li v-for="joke in jokes">
          <p>{{joke.joke}}</p>
        </li>
      </ul>

    </template>


    <script src = "app.js" charset="utf-8"></script>
  </body>
 
  </html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.