Skip to content

Add vuex example #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion example/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<Hello ref="helloComponent" />
<World />
<button @click="greet">Greet</button>

Clicked: {{ $store.state.count }} times, count is {{ evenOrOdd }}.
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="incrementIfOdd">Increment if odd</button>
<button @click="incrementAsync">Increment async</button>
</div>
</template>

Expand All @@ -16,6 +22,7 @@ import Vue from 'vue'
import Component from '../lib/index'
import Hello from './Hello.vue'
import World from './World'
import { mapGetters, mapActions } from 'vuex'

// We declare the props separately
// to make props types inferable.
Expand All @@ -29,7 +36,16 @@ const AppProps = Vue.extend({
components: {
Hello,
World
}
},
// mapGetters & mapActions example
computed: mapGetters([
'evenOrOdd'
]),
methods: mapActions([
'increment',
'decrement',
'incrementAsync'
])
})
export default class App extends AppProps {
// inital data
Expand All @@ -54,6 +70,11 @@ export default class App extends AppProps {
this.$refs.helloComponent.sayHello()
}

// direct dispatch example
incrementIfOdd() {
this.$store.dispatch('incrementIfOdd')
}

// dynamic component
$refs!: {
helloComponent: Hello
Expand Down
2 changes: 2 additions & 0 deletions example/example.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Vue from 'vue'
import App from './App.vue'
import store from './store'

// mount
new Vue({
el: '#app',
store,
render: h => h(App, {
props: { propMessage: 'World' }
})
Expand Down
49 changes: 49 additions & 0 deletions example/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Vue from 'vue'
import Vuex, { ActionContext } from "vuex"
interface CounterState{
count: number
}

Vue.use(Vuex)

const state = {
count: 0
}

const mutations = {
increment (state: CounterState) {
state.count++
},
decrement (state: CounterState) {
state.count--
}
}

const actions = {
increment: (context: ActionContext<CounterState, any>) => context.commit('increment'),
decrement: (context: ActionContext<CounterState, any>) => context.commit('decrement'),
incrementIfOdd (context: ActionContext<CounterState, any>) {
if ((context.state.count + 1) % 2 === 0) {
context.commit('increment')
}
},
incrementAsync (context: ActionContext<CounterState, any>) {
return new Promise((resolve) => {
setTimeout(() => {
context.commit('increment')
resolve()
}, 1000)
})
}
}

const getters = {
evenOrOdd: (state: CounterState) => state.count % 2 === 0 ? 'even' : 'odd'
}

export default new Vuex.Store({
state,
getters,
actions,
mutations
})
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy