-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vuex] already installed. Vue.use(Vuex) should be called only once. #731
Comments
I have the same problem |
It already is answered several days ago in the other issue (#729), and it looks like exactly same situation. |
In this case, the external base files are just only JQuery and VueJS; Vuex arrives after ajax |
Vuex automatically call Workaround is just removing |
Well, not really.The point is that In fact, in this instance, the "previously installed" check in vuex is falsely triggered. Not sure how to improve this atm though. |
I have the same problem :( @LinusBorg @ktsn |
I have troubles with the same |
Are you all using two different versions of Vue on your website? Why? |
In this case, I don't have to much control. we developed a checkout that can be injected in any merchants page, using the example I show you. We ask the merchant to call our endpoint and include the html, js to their page. I have control over the Vue instance that is called through Ajax. There is a way I can create a scope/namespace? so both Vue instances can live in the same page?? or how can I prevent my instance to instantiate again and use the one already created. |
Oh, I understand. |
Like |
In this situation, we don't want Vuex to use global Vue since it's loaded as ESModule. So I think we can skip implicit registration if there is if (
typeof window !== 'undefined' && window.Vue
&& typeof exports !== 'object' && typeof module === 'undefined' // commonjs
&& typeof define !== 'function' && !define.amd // amd
) {
install(window.Vue)
} But not sure this is right direction. And I noticed this may break apps that load Vue in global but Vuex by any module system 😞 |
Can't install Vuex through the instance with the DIV ID?. |
Hmm, I think we only have a dirty solution like below: // bootstrap.js
// Note that you have to load this code as soon as possible in your entire code
// Retain global Vue instance and remove it from `window` temporally
const GlobalVue = window.Vue
window.Vue = null
// Load Vuex code
require('vuex')
// Restore the global Vue instance
window.Vue = GlobalVue // main.js (Your project's entry point)
import './bootstrap'
import Vue from 'vue'
// ...
new Vue({
el: '#app',
// ...
}) |
Working just:
on main.js |
But if you do not restore the global Vue, it would affect the merchant app, doesn't it? |
You're absolutely right, but that way I keep getting the error message. This working for me:
|
I also get this error message "[vuex] already installed. Vue.use(Vuex) should be called only once." Hello.vue
stores/index.js
main.js
that is the all.I can success to use this plugin in the my project.But I get error message like below.I feel not comfortable for that.someone can help me how to resolve this issus? |
I got this working by hacking vuex.esm.js Just commented out
Not the cleanest solution but worked out fine. I think the logic is that if it sees window.Vue, it should make itself available to everyone, but I could be wrong. Maybe Vuex can only really run one instance. |
@posva The solution by @anthonywebster does not solve my issue. That's why I added my own comment and shared a hack. Please note, that I will be running my app inside another web page that may have included a different version vue js via a script tag. |
Can't we just remove(or add the option) the install part of the code in the .esm bundle? Would that break anything? |
It is technically breaking change. I think it would be compromising point to add |
But what about for use cases like ours where we need to install vuex to the esm Vue instance rather than the window.Vue instance, is our only option is always using a fork version? |
Well, I'm suggesting the option to solve your use case. When you install the ESM Vue constructor with |
Sorry not familiar with that, do you have a reference that I can use to try it out? |
Ah, I'm talking about a new option 😄 Vue.use(Vuex, { force: true }) And we can add a condition here to skip the assertion. export function install (_Vue, options) {
if (!options.force && Vue) {
if (process.env.NODE_ENV !== 'production') {
console.error(
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
)
}
return
}
Vue = _Vue
applyMixin(Vue)
} |
Ah got it, haha, we seem to have been miscommunicating. Looks like a great option. My only concern is, would my esm Vuex, would still be installed to the window.Vue, instead of the esm Vue installed with my bundle? Just trying to avoid the possibility that the host page might be using a different version of both Vue and Vuex, and don't want to introduce any conflicts. I'm not even sure how my app is working even after I just commented out the whole install script. |
Would it be possible to check if Vuex is already installed so we can only use it if it isn't? Something like: if (!Vue.vuex_installed_or_something) {
Vue.use(Vuex)
} edit to clarify, something along these lines: export function install (Vue) {
if (Vue._vuex_installed) {
if (process.env.NODE_ENV !== 'production') {
console.error(
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
)
}
return
}
Vue._vuex_installed = true
applyMixin(Vue)
} Then we could check: if (!Vue._vuex_installed) {
Vue.use(Vuex)
} |
Unfortunately, it would not solve the problem. |
True, it wouldn't directly solve the problem, but it would let developers be able to check if Vuex is already installed so they can avoid doing it a second time. However, what about having Vuex do nothing if it's already installed? Something like changing this to: export function install (_Vue) {
if (!_Vue._vuex_installed) {
applyMixin(_Vue);
_Vue._vuex_installed = true
}
Vue = _Vue
} That shouldn't have any backwards compatibility issues, and it would allow third party libraries to include Vuex without having to know/check whether or not the host app has included it. |
@thatbignerdguy looks like a great idea. But I really just need an option NOT to install Vuex at all to the global window object and just make it available just for my bundle. My widget is installed on other peoples website, and installing it on the window object could wreak havok on that site's own javascript code. |
I came up with the solution of this problem. Already made a PR #914 |
Version
2.0.0
Reproduction link
https://jsfiddle.net/15ww4vog/8/
Steps to reproduce
What is expected?
What is actually happening?
My parent view is loading Vue instance.
and I'm injecting a Child view that also contains its own Vue instance.
The text was updated successfully, but these errors were encountered: