On a problem of circular dependency in the use of vuex

clipboard.png

there is a requirement that the store of vuex should be initialized to obtain the secondary domain address of the web page, which I write down as the variable host. The setting of cookie is relative to the second-level domain name, that is, a.xx.com and b.xx.com share a set of cookie, so setCookie and getCookie functions need to use host, in store. However, some action or mutation functions such as login and app status in store need to operate cookie, that is to say, setCookie and getCookie functions are also referenced in store. This forms circular dependency. How to solve this problem?

I have thought about solving the problem from two directions: either store does not use host, to write in global variables, but it is too dirty, and the test environment is not easy to deal with; or login (recording state settings such as token), app (some Boolean values) setCookie and getCookie functions are written in specific pages instead of store, but I also feel that it is not elegant and the management is not centralized (currently login and other operations are all written in mutation)

in addition, host must be obtained dynamically, because the host is not fixed. It may be a system similar to sass that uses

for many domain names.

ask for advice

Apr.05,2021

get host, when initializing it can be used directly when logging in, and then you can get it later. This is not a circular reference, because the host in your store is not obtained by manipulating the function of cookie. And the second-level domain name of the web page is also easy to obtain. Even if you haven't got the host in store when you log in, you can also directly take the second-level domain name


host as a parameter and pass it in


host as a parameter

.
Menu