0

I am currently using Vue JS and I am not using any router. I am not using stand-alone build and would like to implement with webpack and use it with .vue extension. Looking for better way to render a Vue template using a variable. For example :

// App.Vue
var Loading = {
   template: `<div>Loading...</div>`
}

// main.js
var currentView = Loading;

new Vue({
  el: "#app",
  render(h) {
    return h(currentView);
  }
});

The above example perfectly works fine without any issues. Would like to understand if there is a better way to handle.

Let's say if currentView contains a string like

currentView = "Loading"

render(h) {
  return h(eval(currentView)); // works
  return h([currentView]); // template not found or mount error 
}

How can we replace the value to the template before its passed to render function ?

Thanks in advance.

1 Answer 1

1

h is Vue's createElement is use to create virtual Dom element. Same as browser's createElement which use to create real Dom element. So it only work with virtual Dom not string. If you want your second example work, you must register a component named "Loading".

var Loading = {
   template: `<div>Loading...</div>`
}

// main.js
var currentView = "Loading";

new Vue({
  el: "#app",
  render(h) {
    return h(currentView);
  },
  components: {
     Loading: Loading //register Loading component as "Loading"
  }
});

Or register it as global Loading component

Vue.component("Loading", Loading);
Sign up to request clarification or add additional context in comments.

Comments

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.