Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could Vue.js have templateUrl configure just like Angular.js do?

I love the simplicity of Vue.js, but I don't want to complexify it with browserify or webpack. I prefer something like templateUrl in Angular, so I can serve the partial page(usually a component) with Nginx directly. How could I set up this? It's not suggested officially, hard to get help there.

like image 373
Kane Blueriver Avatar asked Oct 10 '15 10:10

Kane Blueriver


1 Answers

Vue doesn't have anything built in for this specifically as far as I can tell but you'd be able to use async components to fake it if you wanted to.

Vue.component('example', function (resolve, reject) {
  $.get('templates/example.html').done(function (template) {
    resolve({
      template: template
    })
  });
});

You'd also be able to do something like this as well in your HTML.

<div id="app"></div>

<template id="example">
  <div>
    <h1>{{ message }}</h1>
  </div>
</template>

Then you can do like:

new Vue({
  el: '#app',
  components: {
    example: {
      template: '#example',
      data: function () {
        return {
          message: 'Yo'
        }
      }
    }
  }
});

Though, I think taking the time to get comfortable with browserify or webpack is well worth the investment. Especially because you can use vueify.

like image 160
Bill Criswell Avatar answered Nov 15 '22 04:11

Bill Criswell