前提
rails webpacker:install
rails webpacker:install:vue
上記のコマンドを実行し、RailsアプリケーションにVue.jsをインストールしたのですが、デフォルトで用意されている'Hello Vue!'が表示されず困っています。よろしければお力添えをお願いいたします🙏
実現したいこと
localhost:3000/home
にアクセスした際に'Hello Vue!'
が表示されるようにしたいです。
発生している問題・エラーメッセージ
Vue.js devtoolsではVue.js not detected
と表示される。
chromeのコンソール
console
1Uncaught TypeError: vue__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor 2 at HTMLDocument.<anonymous> (hello_vue.js:12:1)
該当のソースコード
config/routes.rb
ruby
1Rails.application.routes.draw do 2 get 'home', to: 'home#index' 3end
app/controoler/home_controller.rb
ruby
1class HomeController < ApplicationController 2 def index 3 end 4end
app/views/home/index.html.erb
ruby
1<%= javascript_pack_tag 'hello_vue' %>
app/javascript/app.vue
vue
1<template> 2 <div id="app"> 3 <p>{{ message }}</p> 4 </div> 5</template> 6 7<script> 8export default { 9 data: function () { 10 return { 11 message: "Hello Vue!" 12 } 13 } 14} 15</script> 16 17<style scoped> 18p { 19 font-size: 2em; 20 text-align: center; 21} 22</style>
app/javascript/packs/hello_vue.js
js
1/* eslint no-console: 0 */ 2// Run this example by adding <%= javascript_pack_tag 'hello_vue' %> (and 3// <%= stylesheet_pack_tag 'hello_vue' %> if you have styles in your component) 4// to the head of your layout file, 5// like app/views/layouts/application.html.erb. 6// All it does is render <div>Hello Vue</div> at the bottom of the page. 7 8import Vue from 'vue' 9import App from '../app.vue' 10 11document.addEventListener('DOMContentLoaded', () => { 12 const app = new Vue({ 13 render: h => h(App) 14 }).$mount() 15 document.body.appendChild(app.$el) 16 17 console.log(app) 18}) 19 20 21// The above code uses Vue without the compiler, which means you cannot 22// use Vue to target elements in your existing html templates. You would 23// need to always use single file components. 24// To be able to target elements in your existing html/erb templates, 25// comment out the above code and uncomment the below 26// Add <%= javascript_pack_tag 'hello_vue' %> to your layout 27// Then add this markup to your html template: 28// 29// <div id='hello'> 30// {{message}} 31// <app></app> 32// </div> 33 34 35// import Vue from 'vue/dist/vue.esm' 36// import App from '../app.vue' 37// 38// document.addEventListener('DOMContentLoaded', () => { 39// const app = new Vue({ 40// el: '#hello', 41// data: { 42// message: "Can you say hello?" 43// }, 44// components: { App } 45// }) 46// }) 47// 48// 49// 50// If the project is using turbolinks, install 'vue-turbolinks': 51// 52// yarn add vue-turbolinks 53// 54// Then uncomment the code block below: 55// 56// import TurbolinksAdapter from 'vue-turbolinks' 57// import Vue from 'vue/dist/vue.esm' 58// import App from '../app.vue' 59// 60// Vue.use(TurbolinksAdapter) 61// 62// document.addEventListener('turbolinks:load', () => { 63// const app = new Vue({ 64// el: '#hello', 65// data: () => { 66// return { 67// message: "Can you say hello?" 68// } 69// }, 70// components: { App } 71// }) 72// }) 73
試したこと
vueの再インストールや、利用しているコンテナの再起動をしましたが、解決しませんでした。
補足情報(FW/ツールのバージョンなど)
動作環境
Docker Desktop for mac
docker-compose.yml
yml
1version: "3" 2services: 3 app: 4 build: ./ 5 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 6 ports: 7 - '3000:3000' 8 volumes: 9 - ./:/memo-app 10 depends_on: 11 - db 12 13 db: 14 image: mysql:8.0 15 environment: 16 MYSQL_ROOT_PASSWORD: password 17 MYSQL_DATABASE: memo_app_development 18 ports: 19 - '3306:3306' 20 command: --default-authentication-plugin=mysql_native_password 21 volumes: 22 - db-data:/var/lib/mysql 23 24volumes: 25 db-data:
Dockerfile(ruby)
Dockerfile
1FROM ruby:2.7.1 2RUN apt-get update -qq && apt-get install -y build-essential libpq-dev default-mysql-client 3 4RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && apt-get install -y nodejs 5RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ 6curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ 7echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ 8apt-get update && apt-get install -y yarn 9 10RUN mkdir /memo-app 11WORKDIR /memo-app 12 13COPY Gemfile* /memo-app/ 14RUN gem install bundler 15RUN bundle install 16 17RUN rails webpacker:compile 18 19COPY . /memo-app 20
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。