0

I am trying to use Vue to make a bootstrap modal dynamic...

Component HTML:

 <div id="commentList">
     <comment-list
         v-for="comment in comments"
         :key="comment.id"
         :content="comment.content"
     ></comment-list>
 </div>

JS

<script>
 const app = Vue.createApp({
        data() {
            return {
                comments: []
            };
        },
        created () {
            this.fetchComments();
        },
        methods: {
            fetchComments: function () {
                $.ajax({
                    url: '/activity/head/52',
                    method: 'GET',
                    success: function (data) {
                        this.comments = data;
                    }
                });
            }
        }
    });

    app.component('comment-list', {
        props: ['content'],
        delimiters: ['{:', ':}'],
        template:
            `
                <div class="row my-4">
                    <div class="col-1"><img src="/photo-alex.jpg" class="rounded-circle" style="width:38px; height: 38px"></div>
                    <div class="col-11 p-3 text-muted rounded bg-white border">
                        {:content:}
                    </div>
                </div>
            `
    });

    app.mount('#commentList');
</script>

The AJAX request isn't being executed, so that's the first issue I am encountering, thoughts?

AJAX response:

[
  {
    id: 76,
    ago: "in 4 hours",
    user: "Alex",
    content: "THIS IS A COMMENT"
  }
]
4
  • I've changed some code and fixed a few issues which were shown in console.log() the AJAX is now being called, but no errors in the console.log and no results are shown??? Ideas? Commented Nov 16, 2020 at 20:01
  • Could you create a minimal reproducible example which would repro the issue. I suggest using codesanbox.io or similar. What you have should work. Although this might be related to using Vue 2 syntax in Vue 3. Not yet versed in 3. Just to make sure, try using latest vue 2 instead of whatever version you're using. Commented Nov 16, 2020 at 20:12
  • Sorry my bad, I pasted from the wrong window initially...I had some simple errors caused by copy-paste from another window, which I also ran locally and didn't notice. The above is error free but also not working. Commented Nov 16, 2020 at 20:12
  • 1
    Nevermind, I spotted the issue. this in fetchData is not the Vue instace. I'll post an answer. Commented Nov 16, 2020 at 20:15

1 Answer 1

2

You're using a normal function for $ajax.success. Inside it, this is that function's context, not the Vue instance. Change it to an arrow function so it has access to the outer scope, which will allow it to assign the response to the Vue instance's comments. Basically:

fetchComments: function() {
  $.ajax({
    url: '/activity/head/52',
    method: 'GET',
    success: function(data) {
      this.comments = data;
    }
  });
}

Should become:

fetchComments() {
  $.ajax({
    url: '/activity/head/52',
    method: 'GET',
    success: data => {
      this.comments = data;
    }
  });
}

Note: changing from fetchComments: function(){ ... } to fetchComments() { ... } is irrelevant. They're the same thing, it's just I prefer the shorter syntax.

What matters is changing success from function(data) { ... } to data => { ... }.

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

7 Comments

Still not rendering the list...I feel it's the Vue3 requirement of data() having to be a function now???
Yep, I made a mistake. Just corrected it. The method should be a function(). It's only the ajax call that should be an arrow function. I'll also reformulate to be exact and specific, so it's helpful :)
That didn't do it either...how would I access the property "comments" with it being wrapped in a function?
OK sorry - I missed the fetchComments()
It's ok. You can either write it as methods: { fetchComments() { ... }} or methods: { fetchComments: function() { ... }}. It's exactly the same thing. I personally prefer the first syntax. For me, it's more readable but I'm aware it's a personal choice.
|

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.