最后,我选择使用Vue的ref
指令。这允许从父级引用组件以进行直接访问。
例如
在我的父实例上注册了组件:
var vm = new Vue({
el: '#app',
components: { 'my-component': myComponent }
});
使用引用渲染模板/ html中的组件:
<my-component ref="foo"></my-component>
现在,在其他地方我可以从外部访问该组件
<script>
vm.$refs.foo.doSomething(); //assuming my component has a doSomething() method
</script>