GithubHelp home page GithubHelp logo

benoitongit / vue-on-rails Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 1.0 18 KB

Easy way to mount/destroy Vue.js components with Ruby on Rails and Turbolinks 5

License: MIT License

Ruby 100.00%
rails turbolinks vuejs vue ruby vue-components props

vue-on-rails's Introduction

VueOnRails

Simple and fast way to use Vue.js with Ruby on Rails and Turbolinks 5.

This gem is fantastic to sprinkle your web page with Vue components. Lightweight script (1KB minified) that mounts and destroys Vue components on demand.

It works nicely with:

  • Turbolinks rendering and caching system
  • Javascript loaded asynchronously with defer or async script attribute

It's fully Google PageSpeed friendly since you can use it with defer or async, no need for render-blocking JavaScript :)

If you are building a pure SPA app with Vue and Rails without Turbolinks, you will be better off looking into: https://github.com/adambutler/vuejs-rails

Requirements

Installation

Add this line to your application's Gemfile:

gem 'vue_on_rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install vue_on_rails

Usage

Require vue_on_rails in application.js

//= require vue_on_rails

Mount and destroy Vue components:

# Mount Vue components
document.addEventListener('turbolinks:load', function() {
  VueOnRails.mountComponents();
});

# Destroy Vue components
document.addEventListener('turbolinks:before-render', function() {
  VueOnRails.destroyComponents();
});

VueOnRails.mountComponents() will scan the page and mount components using data-vue-component and data-vue-props.

Example Vue component in my_component.js

MyComponent = {
  props: ['myProp'],
  template: '<div>A custom component with {{myProp}}</div>'
};

Add component in view.html.erb

<%= vue_component 'MyComponent', my_prop: 'awesome prop!' %>
<!-- This will output -->
<div data-vue-component="MyComponent" data-vue-props="{&quot;my-prop&quot;:&quot;awesome prop!&quot;}">
   <div>A custom component with awesome prop!</div>
</div>

View Helper

A view helper generate a div with component and props:

def vue_component(component_name, props = {}, html_options = {})

Example with html tag:

<%= vue_component 'UserComponent', { user_id: 1 }, { class: 'd-inline-block align-middle' } %>
<!-- Becomes: -->
<div data-vue-component="UserComponent" data-vue-props="{'user-id': 1}" class="d-inline-block align-middle">
...
</div>

Props

Kebab-case (hyphen-delimited)

Props are automatically hyphen-delimited to follow vuejs and HTML5 standard.

Dynamic props

<%= vue_component 'MessageComponent', ':my_object': { title: 'Test', subtitle: 'Sweet!' } %>

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/benoitontgit/vue-on-rails.

License

The gem is available as open source under the terms of the MIT License.

vue-on-rails's People

Contributors

benoitongit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

richardchester

vue-on-rails's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.