Created
January 3, 2017 14:39
-
-
Save tamanugi/cc2631f929127901a85526b67a9514ba to your computer and use it in GitHub Desktop.
Vue.jsでFizzBuzzを書いてみる ref: http://qiita.com/tamanugi/items/dc12cb48a2a1d2340e72
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
<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> |
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
# 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 |
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
src | |
├── App.vue | |
├── components | |
│ ├── Buttons.vue | |
│ └── FizzBuzz.vue | |
├── main.js | |
└── store.js |
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
<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> |
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
// 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 } | |
}) |
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
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