Vue实例内部的Vue实例

大家好!

我在这里获得此HTML代码:

// index.html
<div data-init="component-one">
  <...>

 <div data-init="component-two">
   <button @click="doSomething($event)">
 </div>
</div>

如果我正确理解所有内容,这基本上是在另一个Vue实例中引用一个Vue实例。相应的JS代码分为两个文件,如下所示:

// componentOne.js
new Vue(
  el: '[data-init="component-one"]',
  data: {...},
  methods: {...}
);


// componentTwo.js
new Vue(
  el: '[data-init="component-two"]'
  data: {...}
  methods: {
    doSomething: function(event) {...}
  }
);

现在,这个问题是,doSomethingcomponentTwo不会被调用。

但是当我做一些内联的东西(如)时{{ 3 + 3 }},它会像应该的那样进行计算。所以Vue知道有什么。并且它还会删除@click页面加载时元素。

I tried fiddling around with inline-template as well, but it doesn't really work as I'd expect it to in this situation. And I figured it isn't meant for this case anyway, so I dropped it again.

What would the correct approach be here? And how can I make this work the easiest way possible with how it's set up right now?

The Vue version we use is 2.1.8.

Cheers!

小胖2020/04/03 11:45:56

组件二内部的按钮元素在Vue中称为插槽。@click指令值的求值发生在父组件(组件一,承载组件二)中。因此,您需要在该处(在组件一处)声明单击处理程序。

如果要在第二组件内部处理该处理程序,则应在其(第二组件)模板中为slot元素声明一个click指令,然后将处理函数传递为例如pop。

祝好运。