Vuex: Vuex hasModule

Created on 17 Jun 2020  路  2Comments  路  Source: vuejs/vuex

Version

3.4.0

Reproduction link

https://codesandbox.io/s/hasmodule-inconsistent-with-string-x58fh?file=/src/App.vue

Steps to reproduce

Check for the existence of a nested module with a string, vs an array path.

What is expected?

I would expect checking with a string or an equivalent array to produce the same result.

What is actually happening?

Checking hasModule with a string returns false, while checking it with an equivalent array returns true.


I suppose this could be intentional, but it seems like a bug. If it's not intentional it seems like a simple fix:

Line 219 in src/store.js currently is:

if (typeof path === 'string') path = [path]

It seems like simply changing that to the following will fix the problem:

if (typeof path === 'string') path = path.split("/")

Thoughts?

documentation

Most helpful comment

Thanks for the report! This is intended at the moment. You can't pass single string value for nested module, in that case you must pass an array. It goes same for registerModule. If you want register nested module, you should pass ['module', 'nested'] instead of 'module/nested'.

I remember we had PR to enable this, but actually, the nested state structure and namespace is different thing. Technically, you could have module called test/regular (not nested, this is single module name. It is possible to include "slash" in module name). So, we dropped that PR due to clarity where if you need to specify nested module, you should always pass array and not string.

But at the same time I understand that this behavior is a bit confusing and easy to miss. I think we could improve our docs, perhaps add warning section for this.

All 2 comments

Upon further inspection, I see that the same if (typeof path === 'string') path = [path] code is used in both registerModule and unregisterModule. This code looks to be pretty old without any updates in a long time, but on a quick inspection updating it in all three places passes all tests.

I'd be happy to open a PR to update this (with additional tests), either strictly in hasModule or in all three places.

Thanks for the report! This is intended at the moment. You can't pass single string value for nested module, in that case you must pass an array. It goes same for registerModule. If you want register nested module, you should pass ['module', 'nested'] instead of 'module/nested'.

I remember we had PR to enable this, but actually, the nested state structure and namespace is different thing. Technically, you could have module called test/regular (not nested, this is single module name. It is possible to include "slash" in module name). So, we dropped that PR due to clarity where if you need to specify nested module, you should always pass array and not string.

But at the same time I understand that this behavior is a bit confusing and easy to miss. I think we could improve our docs, perhaps add warning section for this.

Was this page helpful?
0 / 5 - 0 ratings