Skip to content

Instantly share code, notes, and snippets.

@tamanugi
Created January 3, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamanugi/cc2631f929127901a85526b67a9514ba to your computer and use it in GitHub Desktop.
Save tamanugi/cc2631f929127901a85526b67a9514ba to your computer and use it in GitHub Desktop.
Vue.jsでFizzBuzzを書いてみる ref: http://qiita.com/tamanugi/items/dc12cb48a2a1d2340e72
<template>
<div id="app">
<fizz-buzz></fizz-buzz>
<buttons></buttons>
</div>
</template>
<script>
import FizzBuzz from './components/FizzBuzz'
import Buttons from './components/Buttons'
export default {
name: 'app',
components: {
FizzBuzz,
Buttons
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
<template>
<div class="buttons">
<button @click="increment" >+</button>
<button @click="decrement" >-</button>
</div>
</template>
<script>
import {mapActions} from 'vuex'
export default {
methods: mapActions([
'increment',
'decrement'
])
}
</script>
# vue-cli をインストール
$ npm install --global vue-cli
# "webpack" ボイラープレートを使用した新しいプロジェクトを作成する
$ vue init webpack vue-fizzbuzz
# 依存関係をインストールしてgo!
$ cd vue-fizzbuzz
$ npm install
# vuexをインストール
$ npm install --save vuex
$ npm run dev
src
├── App.vue
├── components
│   ├── Buttons.vue
│   └── FizzBuzz.vue
├── main.js
└── store.js
<template>
<div>
<div v-if="fizzbuzz">{{ fizzbuzz }}</div>
<div v-else>{{ $store.state.count }}</div>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: mapGetters([
'fizzbuzz'
])
}
</script>
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store'
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
template: '<App/>',
components: { App }
})
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
count: 1
}
const mutations = {
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
}
const actions = {
increment: ({commit}) => commit('increment'),
decrement: ({commit}) => commit('decrement')
}
const getters = {
fizzbuzz: state => {
let message = ''
if (state.count % 3 === 0) message += 'Fizz'
if (state.count % 5 === 0) message += 'Buzz'
return message
}
}
export default new Vuex.Store({
state,
getters,
actions,
mutations
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment