Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly install Vuetify for Rails?

Javascript Error:

[Vuetify] Multiple instances of Vue detected
See https://github.com/vuetifyjs/vuetify/issues/4068

If you're seeing "$attrs is readonly", it's caused by this
consoleError @ vuetify.js:22001
install @ vuetify.js:16239
Vue.use @ vue.esm.js:4751
install @ vuetify.js:17943
Vue.use @ vue.esm.js:4751
(anonymous) @ application.js:22
__webpack_require__ @ bootstrap 0e105d68ff80c96169ef:19
(anonymous) @ bootstrap 0e105d68ff80c96169ef:62
(anonymous) @ bootstrap 0e105d68ff80c96169ef:62

Tried:

  • Importing from 'vue' instead of from 'vue/dist/vue.esm' as proposed in this github issue. This causes a different problem (runtime-only build for vue without template parsing)

Details:

I have a Rails app where I would like to create a navigation drawer using Vuetify's <v-navigation-drawer>. As per Vuetify's Quick Start Guide, I installed using $ npm install vuetify --save. Then for a functionality check I used their own example in one of my single file components:

The Template

<!-- app/javascript/navigation-drawer.vue -->
<template>
    <div id="nav-drawer">
        <v-navigation-drawer permanent>
            <v-toolbar flat>
                <v-list>
                    <v-list-tile>
                        <v-list-tile-title class="title">
                           Application
                        </v-list-tile-title>
                    </v-list-tile>
                </v-list>
            </v-toolbar>

            <v-divider></v-divider>

            <v-list dense class="pt-0">
                <v-list-tile
                    v-for="item in items"
                    :key="item.title"
                    @click=""
                >
                   <v-list-tile-action>
                       <v-icon>{{ item.icon }}</v-icon>
                   </v-list-tile-action>

                   <v-list-tile-content>
                       <v-list-tile-title>{{ item.title }}</v-list-tile-title>
                    </v-list-tile-content>
                </v-list-tile>
            </v-list>
        </v-navigation-drawer>
    </div>
</template>

<script>
   export default {
       data () {
           return {
               items: [
                { title: 'Home', icon: 'dashboard' },
                { title: 'About', icon: 'question_answer' }
            ],
            right: null
        }
    }
}

Main application.js

import TurbolinksAdapter from 'vue-turbolinks'
import Vue from 'vue/dist/vue.esm'
import BootstrapVue from 'bootstrap-vue'
import Vuetify from 'vuetify'
import NavigationDrawer from '../navigation-drawer.vue'

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(TurbolinksAdapter)
Vue.use(BootstrapVue)
Vue.use(Vuetify)

Vue.component('navigation-drawer', NavigationDrawer)

document.addEventListener('turbolinks:load', () => {
  const app = new Vue({
    el: '[data-behavior="vue"]'
  })
})

NPM vuetify version

$ npm list vuetify
barber-ranks@ /path/to/app/barber-ranks
└── [email protected] 

NPM vue version

$ npm list vue
barber-ranks@ /path/to/app/barber-ranks
├── [email protected] 
└─┬ [email protected]
  └── [email protected] 

Note that this rails project uses both npm and yarn to manage node packages. I am not sure if this could create a conflict.

like image 429
eriel marimon Avatar asked Dec 02 '18 17:12

eriel marimon


People also ask

How do I add Vuetify to my Vue project?

Vue UI installOn the left side of your screen, click on Plugins. Once there, search for Vuetify in the input field and install the plugin.

Can you use Vuetify without Vue?

No, you can't run Vuetify without Vue. The reason is pretty simple, most of Vuetify is built with vue and most of those components require their script to run, so everything that's not entirely css based will not work.


1 Answers

Found in this issue, you can resolve Vue path and make Vuetify instance working with Vue.

In your config > webpack > environment.js add:

const { environment } = require('@rails/webpacker')
const vue =  require('./loaders/vue')

environment.loaders.append('vue', vue)

const resolver = {
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
}
environment.config.merge(resolver)

module.exports = environment

In javascript > application.js, you can now import Vue and Vuetify like this with no error:

import Vue from 'vue'
import Vuetify from 'vuetify'
like image 104
Sovalina Avatar answered Oct 11 '22 05:10

Sovalina