Vuejs2 如何从Vue';s:是吗

Vuejs2 如何从Vue';s:是吗,vuejs2,Vuejs2,我有以下几部分: <template id="test-button-component"> <div class="test-button__container"> This is test button <button @click="clickButton">{{buttonTitle}}</button> </div> </template> <template

我有以下几部分:

<template id="test-button-component">
    <div class="test-button__container">
        This is test button
        <button @click="clickButton">{{buttonTitle}}</button>
    </div>
</template>

<template id="test-button-component2">
    <div class="test-button__container">
         <button></button>
    </div>
</template>
每次
myComponentName
更改为其他组件时,新组件将替换旧组件。我需要的是,是否有任何方法可以获取组件的实例,以便获取当前绑定组件的视图模型实例?

您可以为动态组件的
标记添加一个(例如
ref=“custom”
)。然后通过
this.$refs.custom
引用组件实例

下面是一个简单的示例,在该示例中,只要绑定到
的值发生更改,就会记录组件的数据:

newvue({
el:“#应用程序”,
数据(){
返回{
值:“foo”,
儿童:{
傅:{
名称:“foo”,
模板:“foo”,
数据(){
返回{value:1};
}
},
酒吧:{
名称:'酒吧',
模板:'bar',
数据(){
返回{value:2};
}
}
}
}
},
计算:{
自定义(){
返回this.children[this.value];
}
},
观察:{
自定义(){
这个.$nextTick(()=>{
console.log(此.$refs.custom.$data)
});
}
}
})

福
酒吧

我想做一些类似于
counterFromComponent=componentVM.counter
,如何访问在
Vue.Component({data:function(){counter:123})下定义的属性或方法像这样?在我发布的示例中,
this.$refs.custom.$data
是子组件的
data
properties对象。因此,如果它有
计数器
属性,您可以通过
this.$refs.custom.$data.counter
访问它。
<div :is='myComponentName' ></div>