在此示例中,如何从clicked
在app2
上触发ComponentA
方法。
const app = Vue.createApp({});
app.component('ComponentA', {
template: `<button @click="clicked" class='btn'>Click</button>`
});
const app2 = Vue.createApp({
methods: {
clicked() {
this.clickCount += 1;
}
},
render() {
return Vue.h(app.component('ComponentA'), options);
}
}).mount("#App");
答案 0 :(得分:1)
从按钮单击事件处理程序中尝试发出一个名为clicked
的事件,在渲染函数中,通过在其前面加上on
来定义该事件,并在体内将第一个字母onClicked: (e) => {...}
括起来该功能的运行this.clicked
let options = {}
options.baseUrl = "someurl.com";
const app = Vue.createApp({})
app.component('ComponentA', {
template: `
<button @click="$emit('clicked')" class='btn'>Click</button>
`
});
const app2 = Vue.createApp({
methods: {
clicked() {
console.log("clicked !!!")
this.clickCount += 1;
}
},
render() {
return Vue.h(app.component('ComponentA'), {
onClicked: (e) => {
this.clicked()
}
}, options)
}
}).mount("#app");
<script src="https://unpkg.com/vue@3.0.0-rc.11/dist/vue.global.prod.js"></script>
<div id="app" someVariable='some value'>
</div>