如何正确安装Vuetify for Rails?

时间:2018-12-02 17:45:16

标签: ruby-on-rails vue.js npm yarnpkg vuetify.js

JavaScript错误:

[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

尝试过:

  • 从“ vue”导入,而不是根据this github issue中的建议从“ vue / dist / vue.esm”导入。这会导致另一个问题(没有模板解析的vue的仅运行时构建)

详细信息:

我有一个Rails应用,我想在其中使用Vuetify的<v-navigation-drawer>创建一个导航抽屉。根据{{​​3}},我使用$ npm install vuetify --save安装。然后,为了进行功能检查,我在单个文件组件之一中使用了自己的示例:

模板

<!-- 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
        }
    }
}

主要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可视化版本

$ npm list vuetify
barber-ranks@ /path/to/app/barber-ranks
└── vuetify@1.3.11 

NPM vue版本

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

注意,该rails项目同时使用npmyarn来管理节点包。我不确定这是否会造成冲突。

1 个答案:

答案 0 :(得分:1)

位于this issue中,您可以解析Vue路径并使Vuetify实例与Vue一起使用。

在您的config > webpack > environment.js中添加:

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

在javascript> application.js中,您现在可以像这样正确地导入Vue和Vuetify:

import Vue from 'vue'
import Vuetify from 'vuetify'