0

I have a file app.js with the following vue object:

var app = new Vue({
  el: '#app',
  data: {
    test: ''
  }
});

I also have a separate js file which is loaded after the app.js file.

When the js file contains the following code, the object will update and show on the page.

app.test = 'test'
$(document).ready(function() {

}

However if I move the first line into the document ready callback function then the element doesn't update on the page:

$(document).ready(function() {
  app.test = 'test'
}

I am not quite sure why this is happening... Would someone be able to explain this to me? And potentially let me know of a way so that I can update the element in the document ready callback function.

Thank you.

3
  • Why are you trying to do this to begin with? Commented Dec 30, 2017 at 2:11
  • I'm actually trying to update the value in a websocket event callback function. This just emulated the behavior in a simpler and shorter way. Commented Dec 30, 2017 at 2:18
  • Instead of document.ready, could you use the mounted event in Vue? Commented Dec 30, 2017 at 2:47

1 Answer 1

1

In an isolated example the problem you've described doesn't occur. The problem must be somewhere in the code you've omitted from your question.

const app = new Vue({
  el: '#app',
  data: {
    test: 'foo'
  }
});

$(() => {
 app.test = 'bar';
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ test }}</p>
</div>

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

3 Comments

I apologize for wasting your time. I should have looked further into it before posting.
@Pabi It's no problem, hope you find out where your issue lies.
Turns out the problem arises when I have two instances of the vue object loaded at the same time.

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.