How to write the view when Vue is introduced in Rails?

First, let's display vue on rails vue.

Create controller and view

rails g controller Pages home

The code below can read the files under app/javasript/packs. CSS too

<%= javascript_pack_tag 'hello_vue' %>  
<%= stylesheet_pack_tag 'hello_vue' %>   

erb:app/view/application.html.erb



    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    +<%= javascript_pack_tag 'hello_vue' %> 
    +<%= stylesheet_pack_tag 'hello_vue' %> 
  </head>

  <body>
    <%= yield %>
  </body>
</html>

When I check the browser, ... Image from Gyazo

Next is the process of manipulating vue from the rails view

app/javascript/packs/hello_vue.js


import Vue from 'vue'
import App from '../app.vue'

document.addEventListener('DOMContentLoaded', () => {
  const app = new Vue({
    render: h => h(App)
  }).$mount()
  document.body.appendChild(app.$el)

  console.log(app)
})


// The above code uses Vue without the compiler, which means you cannot
// use Vue to target elements in your existing html templates. You would
// need to always use single file components.
// To be able to target elements in your existing html/erb templates,
// comment out the above code and uncomment the below
// Add <%= javascript_pack_tag 'hello_vue' %> to your layout
// Then add this markup to your html template:
//
// <div id='hello'>
//   {{message}}
//   <app></app>
// </div>


// import Vue from 'vue/dist/vue.esm'
// import App from '../app.vue'

// document.addEventListener('DOMContentLoaded', () => {
//   const app = new Vue({
//     el: '#hello',
//     data: {
//       message: "Can you say hello?"
//     },
//     components: { App }
//   })
// })

If you read the commented out text in the center, comment out the top and uncomment the bottom. And the code below is written to erb as a pace.

app/javascript/packs/hello_vue.js


import Vue from 'vue/dist/vue.esm'
import App from '../app.vue'
import Home from './pages/Home.vue'


document.addEventListener('DOMContentLoaded', () => {
  const app = new Vue({
    el: '#hello',
    data: {
      message: "Can you say hello?"
    },
    components: { App }
  })
})

Browser side

erb:home.html.erb


<div id='hello'>
  {{message}}
  <%# ↓app.loading vue%>
  <app></app>
  <h1>Pages#home</h1>
</div>

Hello.vue and can you say hello? Are displayed and you are done.

It is the end.

reference: https://qiita.com/cohki0305/items/582c0f5ed0750e60c951

Recommended Posts

How to write the view when Vue is introduced in Rails?
[Rails] How to display an image in the view
[Rails] How to write in Japanese
[Rails] How to display information stored in the database in view
How to use credentials.yml.enc introduced in Rails 5.2
How to solve the problem when the value is not sent when the form is disabled in rails and sent
How to write Rails
[Rails] How to write when making a subquery
How to check Rails commands in the terminal
[Ruby] How to prevent errors when nil is included in the operation
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
How to set the display time to Japan time in Rails
[Ruby on Rails] How to write enum in Japanese
How to write Rails validation
How to write Rails seed
How to set when "The constructor Empty () is not visible" occurs in junit
How to write Rails routing
[Rails] How to load JavaScript in a specific view
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
How to set chrony when the time shifts in CentOS7
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] How to write user_id (foreign key) in strong parameter
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to install Swiper in Rails
How to perform a specific process when the back button is pressed in Android Fragment
[Rails] What to do when the view collapses when a message is displayed with the errors method
[Rails / Routing] How to refer to the controller in the directory you created
[rails] How to display parent information in child view in nested relationships
I tried to understand how the rails method "redirect_to" is defined
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
What to do if Cloud9 is full in the Rails tutorial
I tried to understand how the rails method "link_to" is defined
How to solve the unknown error when using slf4j in Java
[Rails] How to reset the database in production environment (Capistrano version)
[Rails 5] How to display the password change screen when using devise
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
How to write when you want to keep line breaks and output while avoiding XSS in Rails
[Rails] How to use the map method
How to use MySQL in Rails tutorial
How to resolve errors when installing Rails 5.1.3
[rails] How to configure routing in resources
How to get the date in java
How to implement ranking functionality in Rails
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Rails] How to get the user information currently logged in with devise
How to find the total number of pages when paging in Java
How to debug the processing in the Ruby on Rails model only on the console
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
How to reference a column when overriding the column name method in ActiveRecord
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
[Rails] How to solve the problem that the default image is overwritten when editing without uploading the image [Active Storage]
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
[Rails] How to decide the destination by "rails routes"
Rails: How to write a rake task nicely
[JavaFX] How to write Eclipse permissions in build.gradle
How to check the logs in the Docker container
[Rails] Various ways to write in seed files