How to properly install Vuetify for Rails?

五迷三道 提交于 2020-12-01 10:59:38

问题


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
└── vuetify@1.3.11 

NPM vue version

$ npm list vue
barber-ranks@ /path/to/app/barber-ranks
├── vue@2.5.17 
└─┬ vue-turbolinks@2.0.3
  └── vue@2.5.17 

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


回答1:


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'


来源:https://stackoverflow.com/questions/53582944/how-to-properly-install-vuetify-for-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!