admin管理员组文章数量:1307909
I've been busy with this for quite some time and haven't e up with a solution. The problem is, I want my module built with Require JS and Vue/Vuex to municate with the outside world.
I don't want to use this
require(["myModule"],function(vm){
vm.$children[0].myMethod()
});
or jquery
// inside app
$(document).trigger("myEvent",payload);
// outside app
$(document).on("myEvent",myHandler);
This is my custom Element
<div id="app">
<customer-select></customer-select>
</div>
and my main.js. I'm using requirejs to load my AMD modules
define(["./app2/app"], function (CustomerSelectApp) {
CustomerSelectApp.init("#app");
});
my app.js
define([ "vue", "jquery", "webjars/nm-customer-select/nm-customer-select",
"css!bootstrap-css" ],
function(Vue, $, customerSelect) {
return {
init : function(targetElement) {
return new Vue({
el : targetElement,
ponents : {
customerSelect : customerSelect
}
});
}
};
});
Is there any way to make the app/ponent municate with the outside world via an event or a reference that I pass in?
Specifically. I want to make a selection in my Vue app and let another App on the same page know about it an receive the selected data to process it further
I've been busy with this for quite some time and haven't e up with a solution. The problem is, I want my module built with Require JS and Vue/Vuex to municate with the outside world.
I don't want to use this
require(["myModule"],function(vm){
vm.$children[0].myMethod()
});
or jquery
// inside app
$(document).trigger("myEvent",payload);
// outside app
$(document).on("myEvent",myHandler);
This is my custom Element
<div id="app">
<customer-select></customer-select>
</div>
and my main.js. I'm using requirejs to load my AMD modules
define(["./app2/app"], function (CustomerSelectApp) {
CustomerSelectApp.init("#app");
});
my app.js
define([ "vue", "jquery", "webjars/nm-customer-select/nm-customer-select",
"css!bootstrap-css" ],
function(Vue, $, customerSelect) {
return {
init : function(targetElement) {
return new Vue({
el : targetElement,
ponents : {
customerSelect : customerSelect
}
});
}
};
});
Is there any way to make the app/ponent municate with the outside world via an event or a reference that I pass in?
Specifically. I want to make a selection in my Vue app and let another App on the same page know about it an receive the selected data to process it further
Share Improve this question edited Jul 5, 2016 at 10:39 Lord Habicht asked Jul 5, 2016 at 9:21 Lord HabichtLord Habicht 1811 silver badge7 bronze badges 1- Maybe this is a little late, but how about using some storage as mediator? something like sessionStorage or localStorage? the problem is that you'll need to keep checking the localstorage to see if there's any data, so you might create maybe a global Window's function and call it inside your ponent with window.readData or something like that. – Javis Perez Commented Sep 28, 2016 at 2:02
1 Answer
Reset to default 7You can try storing the root Vue node as a global variable using window.name
define(["./app2/app"], function (CustomerSelectApp) {
window.VueRoot = CustomerSelectApp.init("#app");
});
Then in other parts of your application you can have
VueRoot.method();
VueRoot.data = value;
VueRoot.$emit('event', data);
I would suggest only doing this with your root node as it would pollute your global namespace otherwise.
本文标签: javascriptAccess Vue method or event from outside the Vue appStack Overflow
版权声明:本文标题:javascript - Access Vue method or event from outside the Vue app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741821930a2399417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论