Vue警告$ listeners和$ attrs为只读 [英] Vue warn $listeners and $attrs is readonly

查看:77
本文介绍了Vue警告$ listeners和$ attrs为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到很多Vue警告,说$ listeners是只读的,而$ attrs是只读的,并且与不同的Bootstrap项或关联.

I am getting a lot of Vue warnings saying $listeners is readonly or $attrs is readonly and related to different Bootstrap items or to .

例如:

[Vue warn]: $attrs is readonly.

found in

---> <BDropdown>
       <Display>
         <App>
           <Root>

我非常确定这与以某种方式两次加载Vue实例有关,但是我真的不知道如何以其他方式进行加载,以便路由仍然有效.

I am very sure it has something to do with loading the Vue instance twice somehow, but I don't really know, how to do it any other way, so that the routing still works.

在我的main.js中,代码如下:

In my main.js the code is as follows:

import Vue from 'vue'
import App from './App'
import router from './router'
import firebase from 'firebase';
import './components/firebaseInit';
import store from './store';
import { i18n } from './plugins/i18n.js'
import BootstrapVue from 'bootstrap-vue'
import VueCarousel from 'vue-carousel';

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

Vue.use(BootstrapVue);
Vue.use(VueCarousel);

let app;
firebase.auth().onAuthStateChanged(user => {
  if(!app) {
    app = new Vue({
      el: '#app',
      router,
      store,
      i18n,
      components: { App },
      template: '<App/>'
    })
  }
})

我的router/index.js代码如下:

My router/index.js code looks as follows:

import Vue from 'vue'
import Router from 'vue-router'
import firebaseApp from '@/components/firebaseInit'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'display',
      component: Display
    },
  ...
  ]
})

// Nav Guards
router.beforeEach((to, from, next) => {
  // check for requiredAuth
  if(to.matched.some(record => record.meta.requiresAuth)) {
    // check if NOT logged in
    ...
    } else {
      // proceed to route
      next();
    }
  } else {
    next();
  }
})

export default router;

由于示例错误来自Display.vue,下面是该代码的摘录:

As the sample errors come from Display.vue, here is an extract of that code:

<template>
  <div>
    <b-row>
      <b-input-group prepend="Category">
        <b-dropdown v-bind:text="currentCategory">
          <b-dropdown-item @click="categroyChanged('All')">All</b-dropdown-item>
          <b-dropdown-item v-for="c in categories" v-bind:key="c" @click="categoryChanged(c)">{{c}}</b-dropdown-item>
        </b-dropdown>
      </b-input-group>
    </b-row>
    <div class="row" v-for="i in Math.ceil(products.length / 3)" v-bind:key="i">
      <div v-for="product in products.slice((i - 1) * 3, i * 3)" v-bind:key="product.id" class="col-md-4 col-6 my-1">
        <b-card 
          v-bind:img-src="product.thumbUrl"
          img-fluid
          img-alt="image"
          overlay>
          <div slot="footer">
            <small class="text-muted">{{product.name}}<br />{{product.price}} VND</small>   
          </div>
            <router-link v-bind:to="{name: 'view-product', params: {product_id: product.product_id}}" class="secondary-content">
              <i class="fa fa-eye"></i>
            </router-link>
            <router-link v-if="isEmployee" v-bind:to="{name: 'edit-product', params: {product_id: product.product_id}}" class="secondary-content">
              <i class="fa fa-pencil"></i>
            </router-link>
            <button @click='addToCart(product)' class='button is-info'><i class="fa fa-cart-arrow-down"></i></button>
        </b-card>
      </div>
    </div>
  </div>
</template>

<script>        
import firebaseApp from './firebaseInit'
import { mapActions } from 'vuex'

export default {
  name: 'display',
  data () {
    return {
      txtSearch: null,
      isLoggedIn: false,
      currentUser: false,
      isEmployee: false,
      products: []
    }
  },
  beforeMount () {
    var db = firebaseApp.firestore();
      db.collection('products').get().then(querySnapshot => {
        querySnapshot.forEach(doc => {
          const data = {
              'product_id': doc.id,
              'article_number': doc.data().article_number,
              'barcode': doc.data().barcode,
              'category': doc.data().category,
              'colour': doc.data().colour,
              'description': doc.data().description,
              'name': doc.data().name,
              'name_ger': doc.data().name_ger,
              'price': doc.data().price,
              'size': doc.data().size,
              'thumbUrl': doc.data().thumbUrl,
          }
          this.products.push(data)
        })
      }) 
    }
  },
  methods: {
    ...mapActions(['addToCart']),

    ... many methods ...

  }
}
</script>

如何摆脱这些错误?

推荐答案

发生这种情况的常见原因有两个:

There are two common reasons why this can happen:

这可能是由于您在其他文件中从不同位置导入Vue的位置相互矛盾,正如其他人所说的那样.因此,例如,您的代码中可能同时包含import Vue from 'vue'import Vue from 'vue.runtime.esm'.

This can be due to contradictory locations of where you are importing Vue from, in different files, as others have said. So you might have both import Vue from 'vue' and perhaps import Vue from 'vue.runtime.esm' in your code, for example.

但这可以导致多个Vue实例 ,这将导致这些错误.

But this can result in multiple instances of Vue, which will cause these errors.

在这种情况下,解决方案是在代码中的任何地方使用import Vue from 'vue',然后在打包系统(webpack,Parcel,汇总等)中使用别名. webpack.config.jswebpack.renderer.config.js(如果您使用的是Electron)中的示例如下:

The solution in this case is to use import Vue from 'vue' everywhere in your code, and then alias it in your packaging system (webpack, Parcel, rollup etcetera). An example of this in webpack.config.js, or webpack.renderer.config.js if you're using Electron, would be:

module.exports = {
  // ...
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1
    }
  }
  // ...
}

请参见 Vue文档中的更多示例.

例如,这可能是因为需要将Vue列入Webpack中的externals之一,而不是.

This can also be because of a need for Vue to be whitelisted as not one of the externals in webpack, for example.

值得注意的是,Bootstrap Vue从2.0到更高版本(肯定是由2.15(可能更早))的更改导致了同样的问题发生.

It is worth noting that changes in Bootstrap Vue from 2.0 to a later version, definitely by 2.15 (and possibly earlier), caused this same problem to occur.

module.exports = {
  // ...
  externals: [
    'fast-glob',
    'jquery',
    'bunyan',
    'yaml',
    'vue',              // Remove this
    'bootstrap-vue',    // Remove this
  // ...
}

这篇关于Vue警告$ listeners和$ attrs为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆