Smashingmagazine Com 2020 01 Data Components Vue Js
Smashingmagazine Com 2020 01 Data Components Vue Js
Search articles...
ABOUT TH E AUTH OR
Using
Using
Using
Usingprops
props
props
propsto
to
to
toshare
share
share
sharedata
data
data
data from parent to child,
Emitting
Emitting
Emitting
Emittingcustom
custom
custom
customevents
events
events
eventsto share data from child
to parent,
Using
Using
Using
UsingVuex
Vuex
Vuex
Vuex to create an app-level shared state.
Read more →
// AccountInfo.vue
<template>
<div id='account-info'>
{{username}}
</div>
</template>
<script>
export default {
props: ['username']
}
</script>
// ProfilePage.vue
<template>
<div>
<account-info :username="user.username" />
</div>
</template>
<script>
import AccountInfo from "@/components/AccountInf
export default {
components: {
AccountInfo
},
data() {
return {
user: {
username: 'matt'
}
}
}
}
</script>
export default {
props: {
username: String
}
}
// GOOD
<account-info :my-username="user.username" />
props: {
myUsername: String
}
// BAD
<account-info :myUsername="user.username" />
props: {
"my-username": String
}
<template>
<div id='account-info'>
<button @click='changeUsername()'>Change User
{{username}}
</div>
</template>
<script>
export default {
props: {
username: String
},
methods: {
changeUsername() {
this.$emit('changeUsername')
}
}
}
</script>
<template>
<div>
<account-info :username="user.username" @chan
</div>
</template>
Let’s try it out. You should see that when you click
the button, the username changes to "new name".
this.$emit('changeUsername', 'mattmaribojoc')
OR
export default {
...
methods: {
changeUsername (username) {
this.user.username = username;
}
}
}
// store/index.js
Vue.use(Vuex);
// main.js
new Vue({
store,
...
1. STATE
mounted () {
console.log(this.$store.state.user.username);
},
2. GETTERS
getters: {
firstName: state => {
return state.user.fullName.split(' ')[0]
}
}
mounted () {
console.log(this.$store.getters.firstName);
}
// in our component
console.log(this.$store.getters.prefixedName("Mr
3. MUTATIONS
mutations: {
changeName (state, payload) {
state.user.fullName = payload
}
},
this.$store.commit("changeName", {
newName: "New Name 1",
});
// or
this.$store.commit({
type: "changeName",
newName: "New Name 2"
});
4. ACTIONS
actions: {
changeName (context, payload) {
setTimeout(() => {
context.commit("changeName", payload);
}, 2000);
}
}
this.$store.dispatch("changeName", {
newName: "New Name from Action"
});
Wrapping Up
Now, you should know three different ways to share
data across components in VueJS: props, custom
events, and a Vuex store.
FURTHER READING
Vuex
Vuex
Vuex
VuexOfficial
Official
Official
OfficialGuide
Guide
Guide
Guide website
VueJS Docs for Props
Props
Props
Propsand Custom
Custom
Custom
CustomEvents
Events
Events
Events
“WTF
WTF
WTF
WTFIsIs
Is
IsVuex?
Vuex?
Vuex?
Vuex?AAAABeginner’s
Beginner’s
Beginner’s
Beginner’sGuide
Guide
Guide
GuideTo
To
To
ToVue’s
Vue’s
Vue’s
Vue’s
Application
Application
Application
ApplicationData
Data
Data
DataStore
Store
Store,”
Store
Store Anthony Gore, Vue.js
Developers
Front-End & UX
Workshops, Online
— Comments 12
Reply ↓
Andrejs
Andrejs
Andrejs
Andrejs wrote J ANU AR Y 24, 2020 21:20
Reply ↓
• Accessibility • Android
• Animation • Apps
• CSS • Design
• E-Commerce • Freebies
• Graphics • HTML
• Illustrator • Inspiration
• iOS • JavaScript
• Performance • Photoshop
• Plugins • React
• Sketch • Typography
• UI • Usability
• Workflow