I am learning Vue.js by writing a small script and ended up with a catch 22 kind of situation.
The JS script (explorer.js
) which handles the Vue.js part:
var vm = new Vue({
el: "#root",
data: {
posts: {},
tags: {}
}
})
qwest.get('/posts.json')
.then(function (xhr, response) {
vm.posts = response;
});
qwest.get('/tags.json')
.then(function (xhr, response) {
vm.tags = response;
});
Case 1: loading explorer.js
early:
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qwest/4.4.6/qwest.min.js"></script>
<link rel="stylesheet" href="/static/css/explorer.css">
<script src="/static/js/explorer.js"></script>
<div id="root">
<button v-for="(content, title) in posts" class="l1 btn">
{{ title }}
<button v-for="tag in content.tags" class="l2 btn">
{{ tag }}
<button v-for="t in tags[tag]" class="l3 btn">
{{ t }}
</button>
</button>
</button>
</div>
I get a Cannot find element: #root
error from Vue.
This is understandable: when explorer.js
runs, the <div id="root">
is not known yet.
Case 2: loading explorer.js
late:
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qwest/4.4.6/qwest.min.js"></script>
<link rel="stylesheet" href="/static/css/explorer.css">
<div id="root">
<button v-for="(content, title) in posts" class="l1 btn">
{{ title }}
<button v-for="tag in content.tags" class="l2 btn">
{{ tag }}
<button v-for="t in tags[tag]" class="l3 btn">
{{ t }}
</button>
</button>
</button>
</div>
<script src="/static/js/explorer.js"></script>
I get a Property or method "data" is not defined on the instance but referenced during render.
error (and other of the same style).
This is also understandable: the v-for
functions try to access data which have not been defined yet.
How (or rather - where) should I load explorer.js
?