Home.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <script>
  2. import Vue from 'vue'
  3. import { MA } from 'vuejs/api/ma-axios'
  4. export default {
  5. props: ['html'], // get the html from parent with props
  6. data() {
  7. return {
  8. template_src: null, // record the prop into data as it will be available in every hooks
  9. template: null // compiled template from html used in render
  10. }
  11. },
  12. beforeMount() {
  13. // console.log('Home beforeMount this.html', this.html);
  14. if(!this.template_src){
  15. if(this.html){ // if html prop is available record it has data
  16. this.template_src = this.html
  17. }else{ // else get it from ajax (e.g. if we didn't load the page from home)
  18. this.getHomeHtml()
  19. }
  20. }
  21. // compile the html src (coming from parent with props or from ajax call)
  22. if(this.template_src){
  23. this.template = Vue.compile(this.template_src)
  24. this.$options.staticRenderFns = []
  25. this._staticTrees = []
  26. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)))
  27. }
  28. },
  29. methods: {
  30. getHomeHtml(){
  31. MA.get(`/materio_user/login_block`)
  32. .then(({data}) => {
  33. this.template_src = data.rendered // record the html src into data
  34. })
  35. .catch(( error ) => {
  36. console.warn('Issue with getHomeHtml', error)
  37. })
  38. }
  39. },
  40. render(h) {
  41. if(!this.template){
  42. return h('span', 'Loading ...')
  43. }else{
  44. return this.template.render.call(this)
  45. }
  46. }
  47. }
  48. </script>
  49. <style lang="scss" scoped>
  50. </style>