I am using Vue.js clientside, and populate a list as follows (this works after an ajax/json api request which I populate with Vue data and methods).

<div id="myDiv1">
  <li v-for="item in items">
    {{ item.title }}
  </li>
</div>

I currently intialise Vue with

var vueApp = new Vue({
...
  data: {
    items: {},
  },
  methods: { ajaxCallAndUpdateVueItems: function(){...} 
  } 
})

ajaxCallAndUpdateVueItems() on some event

However, I would like to preload/render on the server side an initial state of the list (for a few reasons like speed, seo, initialstate first time only). This state varies depending on things like location of the user, which the server calculates.

So I want to send the initial page to the client with

<div id="myDiv1"> 
  <li>Some title1</li>
  <li>Some title2</li>
</div>

Is there any way to somehow initialise the Vue data with the content of myDiv1 for example, so I don't have 2 sets of html I somehow mess with (e.g deleting the initial html block and repopulating via Vue) ?

share|improve this question
    
Are the values in the li tags static? If so, why not hard-code the items in data.items? – Alvin Thompson Nov 4 '16 at 16:43
    
Hi Alvin, they aren't static, and would vary on things like your location – Ian Nov 4 '16 at 16:46
1  
Have your HTML spit out <script>var foobar = [ items ];</script> (where items is your array of items) and initialize the data as items: items. – ceejayoz Nov 4 '16 at 16:52
    
I was hoping to initialize the page with html rather than js for seo reasons, but it is one route I will explore thanks. – Ian Nov 4 '16 at 16:58
1  
Google still crawls data that is in synchronous Javascript. If you do what @ceejayoz said, you'll get the SEO value. It's only when you have to fetch something with AJAX that Google doesn't index it. vuejs.org/guide/ssr.html – Jeff Nov 4 '16 at 20:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.