I currently have a display component called app-display
, which contains a dynamic component inside (by default, it is set to app-empty
):
app.component('appDisplay', {
template: `<component :is="currentComponent"></component>`,
data() {
return {currentComponent: 'appEmpty'}
}
});
My goal is to create a new instance of app-message
, assign a value to the property message
, and then set this instance as the current component for app-display
when a button is clicked.
The following is the browser code related to this question:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue component reference</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app"></div>
<script>
// Main application consists of a button and a display panel
const app = self.Vue.createApp({
template: `
<app-btn></app-btn>
<app-display></app-display>
`
});
// Button component triggers an action
app.component('appBtn', {
template: `
<button v-on:click="setDisplay">Display message</button>`,
methods: {
setDisplay() {
console.log('To set "appMessage" with a specific "message" parameter as the inner component of "app-display", I need assistance here.');
}
}
});
// Component containing a dynamic components
app.component('appDisplay', {
template: `<component :is="currentComponent"></component>`,
data() {
return {currentComponent: 'appEmpty'}
}
});
// Default component displayed initially
app.component('appEmpty', {
template: `<div>I'm empty.</div>`
});
// Custom message component to be displayed on button click
app.component('appMessage', {
template: `
<div>{{ message }}</div>
`,
props: {
message: String
}
});
// Mounting main app to the page
app.mount('#app');
</script>
</body>
</html>
How can I access the app-display
component from within the app-btn
?