Solution: Migration from Vue 2 to Vue 3
Here’s the solution to the migration from Vue 2 to Vue 3 challenge.
We'll cover the following
Solution
The challenge required you to migrate a project from Vue 2 to Vue 3 code. These are the changes required:
For src/main.js
:
- Replace the Vue Import with the
createApp
method and use it to initialize a Vue application. ThecreateApp
method is a factory function that returns a new Vue app instance, so it doesn’t require thenew
keyword.
For src/components/Login.vue
:
- Replace
:email.sync
and:password.sync
withv-model:email
andv-model:password
on theLoginForm
component in the template.
For src/components/LoginForm.vue
:
- Add an
emits
array to the component definition with["update:email", "update:password", "submit"]
values.
For src/components/BaseInput.vue
:
-
Replace the
value
prop withmodelValue
(unless you use a namedv-model
, likev-model:value
). -
Change the value prop from
:value=”value”
to:value=”modelValue”
. -
Remove the
inputFieldListeners
computed andv-on=" inputFieldListeners"
from the input because they aren’t needed anymore. All additional event listeners are forwarded as part of thev-bind=" attrs"
. -
Add the
emits
array with theupdate:modelValue
string.
Let’s run the below widget to understand how this solution works.
Note: The code below may take a while to run. When the server starts, go to the app URL and see the output.
Get hands-on with 1400+ tech skills courses.