Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js - Organize big single page application with multiple views

I'm playing with new MVVM framework - Vue.js (http://vuejs.org/).

It was really nice in simple examples and demos but now I'm trying to create big SPA with multiple views and I'm realizing that the best pattern how to do it is not described in framework's docs.

The main problem is that I don't know how to handle views on different routes.

For example, I'm using Director (https://github.com/flatiron/director) for routing but how can I change views?

var booksCtrl = function () {    var booksViewModel = new Vue({        el: '#books'        data: { ... }        ready: function () {           // hide previous ViewModel and display this one??        }    }); };  var editBookCtrl = function (id) {     var editBookViewModel = new Vue({        el: '#editBook'        data: { ... }        ready: function () {           // hide previous ViewModel and display this one??        }    }); };  var routes = {     '/books': booksCtrl,     '/books/:id/edit': editBookCtrl };  var router = new Router(routes); router.init(); 

Do I need to create separate Vue.js ViewModels and just display:block / display:none them like in this example?

What would be the right way in your opinion? Thanks!

like image 542
Kosmetika Avatar asked Mar 16 '14 16:03

Kosmetika


People also ask

Is VueJS good for large applications?

Vue. js 3 is a solid framework for building applications both large and small. In our series How to Structure a Large Scale Vue.

Is Vue JS gaining popularity?

The increase in popularity can be seen particularly well when analyzing data from 2018 and 2019 when Vue. js saw incredible growth: 229 percent, which is enormous compared to React (34 percent). With 4.4 million monthly downloads on NPM, Vue. js already occupies a significant portion of the market.


1 Answers

Nested subviews can be resolved by using v-view and v-ref.

html

<div id="main">     <div v-view="currentView" v-ref="view"></div> </div> <ul>     <li><a href="#/">top</a></li>     <li><a href="#/nest/view1">nest/view1</a></li>     <li><a href="#/nest/view2">nest/view2</a></li> </ul>  <script id="top" type="x-template">     <div>top view</div> </script>  <script id="nest" type="x-template">     <div>         <span>nest view</span>         <div v-view="subview"></div>     </div> </script> 

javascript

Vue.component('top', Vue.extend({     template: "#top", }));  Vue.component('nest', Vue.extend({     template: '#nest',     components: {         view1: Vue.extend({             template: '<span>this is subview 1</span>',         }),         view2: Vue.extend({             template: '<span>this is subview 2</span>',         }),     },     data: {         subview: "view1",     }, }));  var main = new Vue({     el: "#main",     data: {         currentView: "top",     }, });  var router = new Router({     '/':        function() { main.currentView = 'top' },     '/nest/:view': function(view) {         main.currentView = 'nest';         main.$.view.subview = view;     }, }); router.init(); 

jsfiddle: http://jsfiddle.net/koba04/WgSK9/1/

like image 196
koba04 Avatar answered Sep 29 '22 10:09

koba04