core icon indicating copy to clipboard operation
core copied to clipboard

[Vue warn]: Avoid app logic that relies on enumerating keys on a component instance. The keys will be empty in production mode to avoid performance overhead.

Open rothskeller opened this issue 6 years ago • 24 comments

Version

3.0.0-rc.9

Reproduction link

https://github.com/rothskeller/vuewarnreport

Steps to reproduce

  • git clone
  • yarn install
  • yarn dev
  • click on links in demo

What is expected?

No warning.

What is actually happening?

[Vue warn]: Avoid app logic that relies on enumerating keys on a component instance. The keys will be empty in production mode to avoid performance overhead.


First, note that this is the same symptom as #1991 but not the same cause.

This is happening because of the call to watch(route, ...) which is made in client/src/pages/ID.vue. The route object has within it a reference to the component, so watching the route includes watching the component, which triggers the warning. But watching the route for changes is a typical, and indeed essential, idiom in vue-router which should not result in a console warning. I don't know whether this should be considered a vue-next bug or a vue-router-next bug, but I'm guessing the former.

rothskeller avatar Sep 01 '20 23:09 rothskeller

watch(route) is implicitly deep: true, which traverses arbitrarily deep properties. So technically, this is expected behavior. Also, deep traversing a complex object when you only care about a few properties is wasteful.

Your use case can and should be rewritten using a computed property instead:

const id = computed(() => {
   return Array.isArray(route.params.id) ? route.params.id[0] : route.params.id
})

If you must use a watcher, you should also use watchEffect and avoid deep watches:

watchEffect(() => {
  id.value = Array.isArray(route.params.id) ? route.params.id[0] : route.params.id
})

yyx990803 avatar Sep 02 '20 02:09 yyx990803

@yyx990803 Your response makes sense, but permit me to note that it disagrees with the documentation for view-router-next, which explicitly calls for watching the route object. I do not know how to reconcile between two core Vue projects.

rothskeller avatar Sep 02 '20 02:09 rothskeller

Can you point me the part of the documentation so I can take a look?

posva avatar Sep 02 '20 06:09 posva

@posva I see two references: here and here.

rothskeller avatar Sep 02 '20 11:09 rothskeller

Thanks!

posva avatar Sep 02 '20 12:09 posva

@yyx990803 How about setting watch(route, () => {}, { deep: false }) instead? Can I avoid deep watch as well?

mannok avatar Oct 14 '20 12:10 mannok

watch(route) is implicitly deep: true, which traverses arbitrarily deep properties. So technically, this is expected behavior. Also, deep traversing a complex object when you only care about a few properties is wasteful.

Your use case can and should be rewritten using a computed property instead:

const id = computed(() => {
   return Array.isArray(route.params.id) ? route.params.id[0] : route.params.id
})

If you must use a watcher, you should also use watchEffect and avoid deep watches:

watchEffect(() => {
  id.value = Array.isArray(route.params.id) ? route.params.id[0] : route.params.id
})

我在vue3 中试用了 vue2 的watch: { $route(newVal, oldVal) {...}} 也会看到这个报警, 这个怎么解决?

anming-john avatar Nov 09 '20 03:11 anming-john

vue3 中,这个是devtools 6.0.0 beta 2打印的,我把devtools关闭,这个waring就消失了

luchaoqun123 avatar Nov 12 '20 06:11 luchaoqun123

Close vue-devtools and the warning will disappear. This should be the problem with DevTools

white-007 avatar Nov 30 '20 05:11 white-007

vue3 中,这个是devtools 6.0.0 beta 2打印的,我把devtools关闭,这个waring就消失了

请问怎么关闭 vue-devtools (vue3) 我也遇到了同样的警告信息,页面直接卡死无响应

ChenWeihua123 avatar Apr 02 '21 03:04 ChenWeihua123

vue3 中,这个是devtools 6.0.0 beta 2打印的,我把devtools关闭,这个waring就消失了

same situation with u, it's OK when I closed devtool.

lvsong77 avatar Apr 21 '21 10:04 lvsong77

You can specify watch(route.xx),So you don't have to deep: true,i try it's OK.

WalkAlone0325 avatar Apr 21 '21 12:04 WalkAlone0325

vue3 中,这个是devtools 6.0.0 beta 2打印的,我把devtools关闭,这个waring就消失了

请问怎么关闭 vue-devtools (vue3) 我也遇到了同样的警告信息,页面直接卡死无响应

set devtools in “when u click the extension” mode

jinxinkai avatar May 01 '21 10:05 jinxinkai

https://next.router.vuejs.org/guide/advanced/composition-api.html#accessing-the-router-and-current-route-inside-setup watch( () => route.params.id, async newId => { userData.value = await fetchUser(newId) } )

leer561 avatar Jun 21 '21 09:06 leer561

For me it's because of my Axios requests ...Whenever I use an Axios (or even fetch doesn't matter) I get this warning...It doesn't vanish even with disabling devtools (So It has nothing to do with the devtools). I used interceptors of Axios to handle some error/catch cases but I don't think it has something to do with it because when I comment out those lines it still gives me warning.

mehdinourollah avatar Jun 29 '21 13:06 mehdinourollah

image

Even in the store when I save my current form ( vue component as form) it gives me this...And in the production it removes it so I can't go further

mehdinourollah avatar Jul 05 '21 09:07 mehdinourollah

image I also had this problem, and I didn't listen to the whole route, but I found that if I used the whole route inside the method, I would also get a warning, so I only called what I needed to use, and the warning didn't appear after that.

MrYusheng avatar Aug 09 '21 08:08 MrYusheng

image

Even in the store when I save my current form ( vue component as form) it gives me this...And in the production it removes it so I can't go further

I'm encoutering a similar issue when dispatching an action to store AG-Grid's Model in my Vuex store. Specifically this bit of code store.dispatch('setSelectedShifters', event.api.getModel()); Considering I'm dispatching actions all over the place without issue, I am wondering if this has more to do with something AG Grid is doing? I have confirmed that the issue goes away when I avoid committing this model, but I don't want to do that.

UPDATE:

So I just removed the "CreateLogger" plugin from my store and the warnings went away. It looks like the logger is causing these in certain circumstances.

davevasquez avatar Nov 19 '21 00:11 davevasquez

Are there other known causes of this "Avoid app logic" warning? I am migrating from Vue 2 to 3, and some routes show this warning in the console hundreds, if not thousands of times on one page.

bglaz avatar Jan 28 '22 20:01 bglaz

Are there other known causes of this "Avoid app logic" warning? I am migrating from Vue 2 to 3, and some routes show this warning in the console hundreds, if not thousands of times on one page.

Saving a component and triggering HMR also causes this warning to show up multiple times.

bglaz avatar Jan 31 '22 15:01 bglaz