前提
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
Uncaught TypeError: vue__WEBPACK_IMPORTED_MODULE_0__.default is not a constructor at HTMLDocument.<anonymous> (hello_vue.js:12:1)
該当のソースコード
config/routes.rb
ruby
Rails.application.routes.draw do get 'home', to: 'home#index' end
app/controoler/home_controller.rb
ruby
class HomeController < ApplicationController def index end end
app/views/home/index.html.erb
ruby
<%= javascript_pack_tag 'hello_vue' %>
app/javascript/app.vue
vue
<template> <div id="app"> <p>{{ message }}</p> </div> </template> <script> export default { data: function () { return { message: "Hello Vue!" } } } </script> <style scoped> p { font-size: 2em; text-align: center; } </style>
app/javascript/packs/hello_vue.js
js
/* eslint no-console: 0 */ // Run this example by adding <%= javascript_pack_tag 'hello_vue' %> (and // <%= stylesheet_pack_tag 'hello_vue' %> if you have styles in your component) // to the head of your layout file, // like app/views/layouts/application.html.erb. // All it does is render <div>Hello Vue</div> at the bottom of the page. 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 the project is using turbolinks, install 'vue-turbolinks': // // yarn add vue-turbolinks // // Then uncomment the code block below: // // import TurbolinksAdapter from 'vue-turbolinks' // import Vue from 'vue/dist/vue.esm' // import App from '../app.vue' // // Vue.use(TurbolinksAdapter) // // document.addEventListener('turbolinks:load', () => { // const app = new Vue({ // el: '#hello', // data: () => { // return { // message: "Can you say hello?" // } // }, // components: { App } // }) // })
試したこと
vueの再インストールや、利用しているコンテナの再起動をしましたが、解決しませんでした。
補足情報(FW/ツールのバージョンなど)
動作環境
Docker Desktop for mac
docker-compose.yml
yml
version: "3" services: app: build: ./ command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" ports: - '3000:3000' volumes: - ./:/memo-app depends_on: - db db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: memo_app_development ports: - '3306:3306' command: --default-authentication-plugin=mysql_native_password volumes: - db-data:/var/lib/mysql volumes: db-data:
Dockerfile(ruby)
Dockerfile
FROM ruby:2.7.1 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev default-mysql-client RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && apt-get install -y nodejs RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ apt-get update && apt-get install -y yarn RUN mkdir /memo-app WORKDIR /memo-app COPY Gemfile* /memo-app/ RUN gem install bundler RUN bundle install RUN rails webpacker:compile COPY . /memo-app
まだ回答がついていません
会員登録して回答してみよう