简体   繁体   English

将数据从一个Vue.js实例输出到另一个实例

[英]Output data from one Vue.js instance into another

I have a pretty big page with lots of stuff going on. 我有一个很大的页面,有很多东西在继续。 So i have 2 Vue instances for 2 parts of the page. 所以我有两个页面的2个Vue实例。 How can i bind data from one Vue instance into another? 如何将数据从一个Vue实例绑定到另一个?

This example should show what i am trying to do. 这个例子应该显示我想要做的事情。 (it's not working that way) (这不是那样的)

<div class="app1">...</div>
...
<div class="app2">{{app1.$data.msg}}</div>

var app1 = new Vue({
    el: '.app1',
    data: {msg: "test"}
});

var app2 = new Vue({
    el: '.app2'
});

In advance, I know this isn't the question you are asking, but I don't know why you need two instances of Vue. 事先,我知道这不是你问的问题,但我不知道为什么你需要两个Vue实例。 Why not just bind Vue to the body and treat both the Vue instances as components. 为什么不将Vue绑定到body并将Vue实例视为组件。 It might be difficult to do what you are trying to do, because it was not intended. 你可能很难做你想做的事情,因为它不是故意的。 Maybe it was, I don't know. 也许是,我不知道。 I have set Vue up on the body and I haven't seen a performance hit. 我已经将Vue设置在身体上并且我没有看到性能受到影响。 Here is a JSBin . 这是一个JSBin

HTML HTML

<div class="container">
    <div id="app1">
        <h1>{{ msg }}</h1>
        <input type="text" v-model="msg" class="form-control"/>

    </div>
    <div id="app2">
        <h1>{{ msg }}</h1>
        <input type="text" v-model="msg" class="form-control"/>
    </div>
</div>

JavaScript JavaScript的

var VueComponent1 = Vue.extend({
    template: '#app1',
    data: function(){
        return {
            msg: ""
        }
    }
});

var VueComponent2 = Vue.extend({
    template: '#app2',
    data: function(){
        return {
            msg: ""
        }
    }
});

var app1 = Vue.component('app1', VueComponent1);
var app2 = Vue.component('app2', VueComponent2);
var app = new Vue({
    el: 'body',
    data: { msg: 'Everybody loves Vue.' }
});

If you are looking for a better way to separate you code, you might want to check out this Vue with Browserify example . 如果您正在寻找一种更好的方法来分隔您的代码,您可能希望使用Browserify示例查看此Vue

Laracasts has a free series on Vue which has been pretty informative as well. Laracasts有一个关于Vue 的免费系列 ,它也提供了相当丰富的信息。

I am still looking for the best solution. 我仍然在寻找最好的解决方案。 The following feels a bit hacky but it works. 以下感觉有点hacky但它​​的工作原理。

You can use the watch option to listen for the change of an expression and trigger a function. 您可以使用watch选项来侦听表达式的更改并触发函数。 In this function you can update the desired data of another Vue instance. 在此功能中,您可以更新另一个Vue实例的所需数据。 In you example we would do this: 在你的例子中我们会这样做:

var app1 = new Vue({
  el: '.app1',
  data: {msg: 'test'},
  watch: {
    'msg': function() { app2.msg = this.msg; }
  }
});

var app2 = new Vue({
  el: '.app2',
  data: { msg: app1.$data.msg },
  watch: {
    'msg': function() { app1.msg = this.msg; }
  }
});

You can see this at work in this jsbin . 你可以在这个jsbin中看到这个。

Moreover, I am wondering if you even need to do this. 而且,我想知道你是否甚至需要这样做。 If this was a real-life-situation there could be better ways to handle this avoiding this hacky solution. 如果这是一个真实的情况,可以有更好的方法来处理这个避免这种hacky解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Vue.JS中将数据从一个组件推送到另一个组件 - Unable to push data from one component to another in Vue.JS 如何将数据从一个组件传递到另一个组件并将该数据存储到 vue.js 中的数组中? - How to pass data from one component to another component and store that data into an array in vue.js? Vue.js 将数据从插槽传递到组件实例 - Vue.js pass data from slot into component instance Vue.js - 从组件内的根实例访问数据 - Vue.js - access data from root instance within component Vue.js:将数据从一个子组件传递到另一个具有相同父组件的组件? - Vue.js: pass data from one child component to another with the same parent? 如何将数据(书籍数组长度)从一个组件传递到 vue.js 中的另一个组件? - How to pass data(books array length) from one component to another component in vue.js? vue.js中如何将一个组件的卡数据绑定到另一个组件卡? - How to bind card data from one component to another component card in vue.js? 将数据从一个 Vue.js 组件传递到 Laravel 中的另一个组件的最佳实践 - best practice for passing data from one Vue.js component to another in Laravel Vue.js 显示从另一个组件调用一个组件 - Vue.js Display call one component from another component Vue.js:如何触发从一个组件到另一个组件的方法 - Vue.js : howto trigger method from one component to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM