Complete the code to define a higher-order component that wraps a base component.
<script setup> import BaseComponent from './BaseComponent.vue' const withWrapper = (Component) => { return { components: { Component }, template: `<div class=\"wrapper\"><[1] /></div>` } } const WrappedComponent = withWrapper(BaseComponent) </script>
The higher-order component returns a new component that renders the passed Component inside a wrapper div. The blank should be filled with Component to render it.
Complete the code to pass props from the higher-order component to the wrapped component.
<script setup> import BaseComponent from './BaseComponent.vue' const withWrapper = (Component) => { return { components: { Component }, props: ['msg'], template: `<div class=\"wrapper\"><Component :msg=[1] /></div>` } } const WrappedComponent = withWrapper(BaseComponent) </script>
The higher-order component receives a prop named msg and passes it down to the wrapped component using :msg="msg". The blank should be filled with msg to bind the prop correctly.
Fix the error in the higher-order component by completing the code to correctly define the component's setup function.
<script setup> import BaseComponent from './BaseComponent.vue' const withWrapper = (Component) => { return { components: { Component }, setup() { const message = 'Hello from HOC' return { [1] } }, template: `<div><Component :msg=\"message\" /></div>` } } const WrappedComponent = withWrapper(BaseComponent) </script>
The setup function must return an object with the properties to expose to the template. Returning { message } exposes the message variable.
Fill both blanks to create a higher-order component that adds a click counter to the wrapped component.
<script setup> import BaseButton from './BaseButton.vue' import { ref } from 'vue' const withClickCounter = (Component) => { return { components: { Component }, setup() { const count = ref(0) const increment = () => count.value++ return { count, [1] } }, template: `<div><Component @click=[2] :count=\"count\" /></div>` } } const EnhancedButton = withClickCounter(BaseButton) </script>
The setup function returns increment to expose the click handler. The template binds the @click event to increment to increase the count.
Fill all three blanks to create a higher-order component that logs a message when the wrapped component is mounted and passes a prop.
<script setup> import { onMounted } from 'vue' import BaseCard from './BaseCard.vue' const withLogger = (Component) => { return { components: { Component }, props: ['title'], setup(props) { const title = props.title onMounted(() => { console.log([1]) }) return { [2] } }, template: `<div><Component :title=[3] /></div>` } } const LoggedCard = withLogger(BaseCard) </script>
The onMounted hook logs a message string. The setup returns title to expose the prop. The template binds the title prop to the wrapped component.