Fun with Vuex and Router

A toy app that uses vuex to track some minor state of an app. Need to add actions to interact with a remote service, but that is going to be missing until I can get APIStar 0.4 up and running.
master
androiddrew 7 years ago
parent 8d79dcb71e
commit bd35bbeab4

@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="shortcut icon" href="<%= webpackConfig.output.publicPath %>favicon.ico"> <link rel="shortcut icon" href="<%= webpackConfig.output.publicPath %>favicon.ico">
<title>vue_fun</title> <title>Vue fun</title>
</head> </head>
<body> <body>
<noscript> <noscript>

@ -0,0 +1,32 @@
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'Central',
props: {
msg: String,
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

@ -1,57 +0,0 @@
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<p>
For guide and recipes on how to configure / customize this project,<br>
check out the
<a href="https://github.com/vuejs/vue-cli/tree/dev/docs" target="_blank">vue-cli documentation</a>.
</p>
<h3>Installed CLI Plugins</h3>
<ul>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank">babel</a></li>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank">eslint</a></li>
</ul>
<h3>Essential Links</h3>
<ul>
<li><a href="https://vuejs.org" target="_blank">Core Docs</a></li>
<li><a href="https://forum.vuejs.org" target="_blank">Forum</a></li>
<li><a href="https://chat.vuejs.org" target="_blank">Community Chat</a></li>
<li><a href="https://twitter.com/vuejs" target="_blank">Twitter</a></li>
</ul>
<h3>Ecosystem</h3>
<ul>
<li><a href="https://router.vuejs.org/en/essentials/getting-started.html" target="_blank">vue-router</a></li>
<li><a href="https://vuex.vuejs.org/en/intro.html" target="_blank">vuex</a></li>
<li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank">vue-devtools</a></li>
<li><a href="https://vue-loader.vuejs.org/en" target="_blank">vue-loader</a></li>
<li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String,
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

@ -5,10 +5,26 @@ Vue.use(Vuex);
export default new Vuex.Store({ export default new Vuex.Store({
state: { state: {
msg: "Hello Vue...Bitches",
safelyStoredNumber: 0
},
getters: {
msg: state => state.msg,
safelyStoredNumber: state => state.safelyStoredNumber,
storedNumberMatches(state) {
return matchNumber => {
return state.safelyStoredNumber === matchNumber;
}
}
}, },
mutations: { mutations: {
incrementStoredNumber(state) {
state.safelyStoredNumber++
},
setStoredNumber(state, newNumber) {
// newNumber is the payload passed in
state.safelyStoredNumber = newNumber
}
}, },
actions: { actions: {

@ -1,18 +1,46 @@
<template> <template>
<div class="home"> <div class="home">
<img src="../assets/logo.png"> <Central :msg="msg"/>
<HelloWorld msg="Welcome to Your Vue.js App"/> <p @click="incrementStoredNumber"> The safely stored number: {{ safelyStoredNumber }}</p>
<input type="text" name="number" v-model="number" value="">
<button @click="setStoredNumber" type="button" name="button">Set the stored number</button>
</div> </div>
</template> </template>
<script> <script>
// @ is an alias to /src // @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'; import Central from '@/components/Central.vue';
import { mapGetters, mapMutations } from 'vuex'
export default { export default {
name: 'home', name: 'home',
data() {
return { number: 0 }
},
components: { components: {
HelloWorld, Central,
},
computed: {
...mapGetters([
// Mounts the msg getter to the she scope of this components `msg`
'msg',
// 'safelyStoredNumber'
]),
// Alternatively we could do this instead to access the getters
safelyStoredNumber() {
return this.$store.getters.safelyStoredNumber
}
}, },
methods: {
...mapMutations([
'incrementStoredNumber'
]),
// Alternatively we could also do This
setStoredNumber() {
this.$store.commit('setStoredNumber', this.number)
this.number = 0
}
}
}; };
</script> </script>

Loading…
Cancel
Save