Last active
April 18, 2017 14:09
-
-
Save pjchender/49e1d5694af4141a3c350aa8880478f1 to your computer and use it in GitHub Desktop.
[Flycan] 20170418 Vue 筆記
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
{ | |
"presets": [ | |
["latest", { | |
"es2015": { "modules": false } | |
}], | |
"stage-2" | |
] | |
} |
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 --> | |
<template> | |
<div id="app"> | |
<img src="./assets/logo.png"> | |
<welcome></welcome> | |
</div> | |
</template> | |
<script> | |
import Welcome from './components/WelcomeComponent.vue' | |
export default { | |
name: 'app', | |
components: { | |
Welcome | |
} | |
} | |
</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; | |
} | |
h1, h2 { | |
font-weight: normal; | |
} | |
ul { | |
list-style-type: none; | |
padding: 0; | |
} | |
li { | |
display: inline-block; | |
margin: 0 10px; | |
} | |
a { | |
color: #42b983; | |
} | |
</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
// ./store/index.js | |
// ES6: import [Variable] from 'package' | |
import Vue from 'vue' | |
import Vuex from 'vuex' | |
// ES5 | |
// const Vue = require('vue') | |
Vue.use(Vuex) | |
const Welcome = { | |
state: { | |
msg: 'Welcome to Your Vue.js App' | |
} | |
} | |
const Essential = { | |
state: { | |
title: 'Essential Links', | |
links:[ | |
{ | |
text:'Core Docs', | |
href:'https://vuejs.org', | |
target:'_blank' | |
}, | |
{ | |
text:'Forum', | |
href:'https://forum.vuejs.org', | |
target:'_blank' | |
}, | |
{ | |
text:'Gitter Chat', | |
href:'https://twitter.com/vuejs', | |
target:'_blank' | |
}, | |
{ | |
text:'Twitter', | |
href:'https://vuejs.org', | |
target:'_blank' | |
}, | |
] | |
} | |
} | |
const Ecosystem = { | |
state: { | |
title: 'Ecosystem', | |
links:[ | |
{ | |
text:'vue-router', | |
href:'http://router.vuejs.org/', | |
target:'_blank' | |
}, | |
{ | |
text:'vuex', | |
href:'http://vuex.vuejs.org/', | |
target:'_blank' | |
}, | |
{ | |
text:'vue-loader', | |
href:'http://vue-loader.vuejs.org/', | |
target:'_blank' | |
}, | |
{ | |
text:'awesome-vue', | |
href:'https://github.com/vuejs/awesome-vue', | |
target:'_blank' | |
}, | |
] | |
} | |
} | |
const store = new Vuex.Store({ | |
strict: true, | |
modules: { | |
Welcome, // Welcome: Welcome 的簡寫 | |
Essential, | |
Ecosystem | |
} | |
}) | |
// ES6 | |
export default store | |
// ES5 | |
// module.export = store |
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.component('listComponent', { | |
template: ` | |
<section class="main"> | |
<input class="toggle-all" type="checkbox" v-model="allDone"> | |
<ul class="todo-list"> | |
<li class="todo" | |
v-for="todo in filteredTodos" | |
:class="{ | |
completed: todo.completed, | |
editing: todo == editing}"> | |
<div class="view"> | |
<input class="toggle" type="checkbox" | |
:checked="todo.completed" | |
@click="switchComplete(todo)"> | |
<label @dblclick="editTodo(todo)"> | |
{{todo.title}} | |
</label> | |
<button class="destroy" | |
@click="removeTodo(todo)"> | |
</button> | |
</div> | |
<input class="edit" type="text" | |
v-model="edit" | |
v-if="todo == editing" | |
v-focus | |
@blur="doneEdit(todo)" | |
@keyup.enter="doneEdit(todo)" | |
@keyup.esc="cancelEdit(todo)"> | |
</li> | |
</ul> | |
</section> | |
`, | |
data: function () { | |
return { | |
editing: null | |
} | |
}, | |
computed: { | |
filteredTodos: function () { | |
return this.$store.getters.filteredTodos | |
}, | |
// allDone:function(){ | |
// return this.$store.getters.allDone | |
// } | |
allDone: { | |
get: function () { | |
return this.$store.getters.allDone | |
}, | |
set: function (val) { | |
this.$store.commit('allDone', val) | |
} | |
}, | |
edit:{ | |
get:function(){ | |
return this.editing.title | |
}, | |
set:function(val){ | |
this.$store.commit('editTitle',{ | |
target:this.editing, | |
value:val | |
}) | |
} | |
} | |
}, | |
methods: { | |
switchComplete: function (todo) { | |
this.$store.commit('switchComplete', todo) | |
}, | |
updateTodo: function (todo) { | |
this.$store.commit('update', todo) | |
}, | |
removeTodo: function (todo) { | |
this.$store.commit('remove', todo) | |
}, | |
editTodo: function (todo) { | |
this.cache = todo.title | |
this.editing = todo | |
}, | |
doneEdit: function (todo) { | |
if (!this.editing) { | |
return | |
} | |
this.editing = null | |
// todo.title = todo.title.trim() | |
if (!todo.title) { | |
this.removeTodo(todo) | |
} | |
}, | |
cancelEdit: function (todo) { | |
this.$store.commit('editTitle',{ | |
target:this.editing, | |
value:this.cache | |
}) | |
this.editing = null | |
} | |
}, | |
directives: { | |
focus: { | |
inserted: function (el) { | |
el.focus() | |
} | |
} | |
} | |
}) |
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 App from './App.vue' | |
import store from './store' // node module 預設回連該 folder 的 index.js | |
new Vue({ | |
el: '#app', | |
render: h => h(App), | |
store | |
}) |
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
var filters = { | |
all: function (todos) { | |
return todos | |
}, | |
active: function (todos) { | |
return todos.filter(function (todo) { | |
return !todo.completed | |
}); | |
}, | |
completed: function (todos) { | |
return todos.filter(function (todo) { | |
return todo.completed | |
}) | |
} | |
} | |
var localStore = { | |
get:function(name){ | |
return JSON.parse(window.localStorage.getItem(name)) | |
}, | |
set:function(name,val){ | |
window.localStorage.setItem(name,JSON.stringify(val)) | |
} | |
} | |
var store = new Vuex.Store({ | |
strict: true, | |
state: { | |
todos:null, | |
visibility:undefined | |
}, | |
getters: { | |
filteredTodos: function(state) { | |
return filters[state.visibility](state.todos) | |
}, | |
remaining: function(state) { | |
return filters['active'](state.todos).length | |
}, | |
allDone: function(state){ | |
return store.getters.remaining === 0 | |
} | |
}, | |
mutations:{ | |
init:function(state){ | |
state.todos = localStore.get('todos') || [] | |
localStore.set('todos',state.todos) | |
}, | |
allDone: function(state,val){ | |
state.todos.forEach(function (todo) { | |
todo.completed = val | |
}) | |
localStore.set('todos',state.todos) | |
}, | |
add:function(state,data){ | |
state.todos.push({title:data,completed:false}) | |
localStore.set('todos',state.todos) | |
}, | |
switchComplete:function(state,data){ | |
var index = state.todos.indexOf(data) | |
state.todos[index].completed = !state.todos[index].completed | |
localStore.set('todos',state.todos) | |
}, | |
// update:function(state,data){ | |
// }, | |
editTitle:function(state,data){ | |
var index = state.todos.indexOf(data.target) | |
state.todos[index].title = data.value | |
localStore.set('todos',state.todos) | |
}, | |
remove:function(state,data){ | |
var index = state.todos.indexOf(data) | |
state.todos.splice(index,1) | |
localStore.set('todos',state.todos) | |
}, | |
removeCompleted:function(state){ | |
state.todos = filters.active(state.todos) | |
localStore.set('todos',state.todos) | |
}, | |
filter:function(state,data){ | |
state.visibility = data | |
}, | |
} | |
}) |
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
<!-- ./components/WelcomeComponent.vue --> | |
<template> | |
<h1>{{ msg }}</h1> | |
</template> | |
<script> | |
// import {} from 'vuex' 表示只載入 Vuex 的其中一個 function | |
import { mapState } from 'vuex' | |
export default { | |
name: 'welcome', // 建議幫每個 component 都命名 | |
computed: { | |
...mapState({ | |
msg: state => state.welcome.msg | |
}) | |
} | |
} | |
</script> | |
<!-- scoped 加上去後,這裡面寫的 CSS 只會限制在這個 component 中 --> | |
<style scoped> | |
h1 { | |
font-weight: bolder; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[vue cli]
vue init webpack-simple
: webpack-simple 是安裝的樣板,如果想要將樣版安裝在新增的資料夾,可以打vue init webpack-simple <folder>
觀念
產生的檔案
Vue 檔的基本樣式:
強化自己的模板
npm i -S vue-router vuex
npm i -D babel-preset-stage-2
安裝好後修改
.babelrc
檔案,加上stage-2