forked from matschik/component-party.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Component composition > Context for vue2 (matschik#153)
- Loading branch information
1 parent
6dd15a3
commit a949e27
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script> | ||
import UserProfile from "./UserProfile.vue"; | ||
export default { | ||
components: { UserProfile }, | ||
provide() { | ||
return { | ||
user: Object.assign(this.user, { | ||
updateUsername: this.updateUsername, | ||
}), | ||
}; | ||
}, | ||
data() { | ||
return { | ||
user: { | ||
id: 1, | ||
username: "unicorn42", | ||
email: "[email protected]", | ||
}, | ||
}; | ||
}, | ||
methods: { | ||
updateUsername(newUsername) { | ||
this.user.username = newUsername; | ||
}, | ||
}, | ||
}; | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<h1>Welcome back, {{ user.username }}</h1> | ||
<UserProfile /> | ||
</div> | ||
</template> |
16 changes: 16 additions & 0 deletions
16
content/4-component-composition/5-context/vue2/UserProfile.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<script> | ||
export default { | ||
inject: ["user"], | ||
}; | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<h2>My Profile</h2> | ||
<p>Username: {{ user.username }}</p> | ||
<p>Email: {{ user.email }}</p> | ||
<button @click="() => user.updateUsername('Jane')"> | ||
Update username to Jane | ||
</button> | ||
</div> | ||
</template> |