LoginSignup
3
4

More than 5 years have passed since last update.

Vue.jsをRails環境下で動かすための環境構築手順

Last updated at Posted at 2019-04-13

参考サイト

https://qiita.com/jnchito/items/30ab14ebf29b945559f6
https://qiita.com/cohki0305/items/582c0f5ed0750e60c951
説明が丁寧でわかりやすい。

開発環境

ruby 2.6.2
Rails 5.2.3

ゴール

ローカル環境下でRuby on RailsにVue.jsを動かす環境を構築する。

手順

通常のプロジェクトを作成
※DBMSにはpostgesqlを指定

$ rails new vue_app -d postgresql
Gemfile
gem 'webpacker', github: 'rails/webpacker'

gemを入れたら、必ずbundle install

webpackerに関する参考サイト
https://blog.tai2.net/webpacker3.html
https://qiita.com/chimame/items/8d3d6f4afea675cffa7d

$ yarn -v

上のコマンドでもしyarnのインストールをしていなかったら、インストールをする。

$ rails webpacker:install

ここでwebpackerをインストール


このコマンドだけで、Vue.jsをインストールできる!

$ rails webpacker:install:vue

hello_vue.jsを使い、Rails上で動作させるための準備

$ rails g controller homes index

適当に、コントローラー・アクション・ビューをコマンドで生成

app/views/layouts/application.html.erb
<%= yield %>
    <%= javascript_pack_tag 'hello_vue' %> 
</body>

このタグを挿入する。
当たり前だが、CDNのインポートは必要ない↓

このタグの挿入は必要ない!!!!!!!!!!!
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
app/views/homes/index.html.erb
<div id="app">
</div>
app/javascript/packs/hello_vue.js
import Vue from 'vue/dist/vue.esm'

const vm = new Vue({
})

※app/javascript/app_vueのコード、ファイルは削除しても構わない。


これで、Rails上でVue.jsを動かす準備は完了。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4