<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Magnus Manske</title>
    <description>The latest articles on DEV Community by Magnus Manske (@magnusmanske).</description>
    <link>https://dev.to/magnusmanske</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F66020%2F2b7e77b4-3eb7-4f61-9163-9f752ea399c3.jpeg</url>
      <title>DEV Community: Magnus Manske</title>
      <link>https://dev.to/magnusmanske</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/magnusmanske"/>
    <language>en</language>
    <item>
      <title>vue.js modules in the browser, the cheap way</title>
      <dc:creator>Magnus Manske</dc:creator>
      <pubDate>Thu, 05 Apr 2018 14:10:42 +0000</pubDate>
      <link>https://dev.to/magnusmanske/vuejs-modules-in-the-browser-the-cheap-way-4dbp</link>
      <guid>https://dev.to/magnusmanske/vuejs-modules-in-the-browser-the-cheap-way-4dbp</guid>
      <description>&lt;p&gt;I really enjoy vue.js, and prefer it to React &lt;i&gt;et al.&lt;/i&gt;&lt;br&gt;
One advantage of vue is the ability to use it in the browser directly, without having to pre-process it on the server, with webpack or similar. But how to separate your vue components in a purely browser-based setup?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can put your JavaScript into &lt;code&gt;.js&lt;/code&gt; files, but how to separate/include &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt;s?&lt;/li&gt;
&lt;li&gt;How to keep JavaScript, templates, and (potentially) CSS together?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ideally, I want to put JS, template code, CSS, and potential dependencies (e.g. non-vue JS libraries) for one component in a single file. However:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; doesn't work if your file does include non-JavaScript code&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; didn't work for me&lt;/li&gt;
&lt;li&gt;RequireJS didn't work either (didn't dwell on it though)&lt;/li&gt;
&lt;li&gt;Up-and-coming ES6 include/require doesn't seem to be there/wide-spread enough yet&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I found a low-tech solution that works for me, and may be of use to some of you, so here goes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I put all my vue components into a single directory, one file each, e.g. &lt;code&gt;component-one.html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Each file consists of &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; etc. tags; just plain HTML, essentially&lt;/li&gt;
&lt;li&gt;To use components in a new app, I simply add this:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;
vue_components.loadComponents (
    ['component-one','component-two','...'] , 
    function(){
        // Initialise router and mount app
        router = new VueRouter({routes}) ;
        app = new Vue ( { router } ) .$mount('#app') ;
    } 
) ;
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;vue_components&lt;/code&gt; object is defined in a separate JS file (my version requires JQuery, for the &lt;code&gt;load&lt;/code&gt; method):&lt;/p&gt;

&lt;pre&gt;
var vue_components = {
    template_container_base_id : 'vue_component_templates' ,
    components_base_url : 'https://my.favourite.server/resources/vue/' ,
    loadComponents : function ( components , callback ) {
        var me = this ;
        var cnt = 0 ;
        $.each ( components , function ( dummy , component ) {
            cnt++ ;
            me.loadComponent ( component , function(){
                if ( --cnt == 0 ) callback() ;
            } ) ;
        } ) ;
    } ,
    loadComponent ( component , callback ) {
        var me = this ;
        var id = me.template_container_base_id + '-' + component ;
        if ( $('#'+id).length &amp;gt; 0 ) return callback() ;
        $('body').append($("&amp;lt;div id='"+id+"' style='display:none'&amp;gt;"));
        var url = me.components_base_url+component+'.html' ;
        $('#'+id).load(url,function(){ callback() }) ;
    }
}
&lt;/pre&gt;

&lt;p&gt;This adds each component in their own &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; at the end of &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, templates, css, code, and all. Only when all components are loaded, the router and app are initialised. AFAICT, this works in every browser that works with JQuery. I just started using this system, and will undoubtedly improve on it, but I welcome any constructive feedback!&lt;/p&gt;

</description>
      <category>vue</category>
      <category>browser</category>
      <category>javascript</category>
      <category>jquery</category>
    </item>
  </channel>
</rss>
