個人的.bashrc配置,這邊備存一下。 顯示完整資訊
Category / 口袋筆記
bash cd到當下目錄位置
在mac中將副檔名改.command後,就可以直接點選並執行該檔內容的script。有次發現想要執行 java -jar helloworld.java 時卻跳出Unable to access jarfile的錯誤訊息,拜訪谷歌大神後發現,原來是執行.command時,當下執行的位置是在home底下而非當下目錄。這時候可以使用下列script去切換到當下目錄位置。 顯示完整資訊
OpenJDK for Windows/Mac
自從Oracle宣告要對Java於商業使用上要收費後,不少企業開發者紛紛轉向OpenJDK。以往在Linux都是透過yum去取得OpenJDK,然後突然間要去找for Windows和Mac版的時候,一時還不知從哪邊找起,這邊做個簡單整理,方便日後下載。 顯示完整資訊
Vue Component v-model 筆記
webpack 新手筆記
webpack主要目的是把多個js檔案合併成一個js檔案。
先使用npm安裝webpack指定版本。
npm install --save-dev webpack@2.2.0.rc.0
下面範例使用commonJS的module system
新增一個webpack.config.js檔案。
// this is path module is from nodejs, // this require is execute from nodejs runtime, not webpack itself // path module provides function to resolve absolute path const path = require('path'); const config = { entry: './src/index.js', output: { // __dirname is a const path: path.resolve(__dirname, 'build'), // must be absolute path filename: 'bundle.js' } }; module.exports = config;
package.json
{ "name": "js_modules", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "build": "webpack" }, "author": "", "license": "ISC", "devDependencies": { "webpack": "^2.2.0-rc.0" } }
這時候我們就可以用 npm run build。這邊不建議安裝webpack globally例如
npm install -g webpack webpack ...
// 雖然可以直接用webpack指令,但因為是全域安裝在自己裝置的開發環境裡面,也就是其他人的環境或其他專案所用的webpack有可能會不同,中間可能會有衝突或非預期結果,不便於開發。所已建議使用npm run {script}的方式執行。
沒有意外的話,我們可以看到 ./build/bundle.js產生在我們的專案目錄下。
webpack在合併js的過程中間其實可以再做許多事情,例如壓縮檔案、打亂程式碼、將ES2015/6/7程式碼改成一般的ES5程式碼等等。當然要做這些事情,webpack可能要額外load一些library。這邊以babel為例 (把ES2015+轉成ES5)。單指引入babel這功能,會需要以下三個modules。

安裝 babel
npm install --save-dev babel-loader babel-core babel-preset-env
前端常見的三種Module System
前端常見的三種Module System。CommonJS常在NodeJS裡見到,而現在前端趨勢是嚮往ES2015。AMD(Asynchronous Module Definition)個人還沒用到過。
他們的syntax 自己常常傻傻搞不清楚 ,下面這張圖看起來明瞭許多。

vuex 新手筆記
安裝vuex
npm install --save vuex
基本的vuex引入
// ./store/store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); export const store = new Vuex.Store({ state: { counter : 0 }, getters: { getCounter: state => { return state.counter; }, getCounterStr: state => { return state.counter + ' counts'; } }, mutations: { increment: state => { state.counter++; }, decrement: state => { state.counter--; }, }, // actions })
// ./main.js import Vue from 'vue' import App from './App.vue' import {store} from './store/store' new Vue({ el: '#app', store, render: h => h(App) })
state和getters及使用mapGetters配合ES6的object spread方式取store裡面state的值
import { mapGetters } from "vuex"; export default { computed: { ...mapGetters(["getCounter", "getCounterStr"]),
counter(){
return this.$store.getters.getCounter;
// return this.$store.state.counter;
} } };
如果是用CLI2且要在專案裡面使用object spread這功能,我們會需要引入babel-preset-stage-2
npm install --save-dev babel-preset-stage-2
// .babelrc { "presets": [["es2015", { "modules": false }], ["stage-2"]] }
mutations同樣也有mapMutations可以使用
import { mapMutations } from "vuex"; export default { methods: { ...mapMutations(["increment", "decrement"]) } };
mutations不能在非同步的狀態下呼叫。假設 mutations裡面的increment是在setTimeout裡面,過1秒後才呼叫callback去改state.counter,如果同時有多個元件呼叫increment,那麼state.counter的狀態將會錯亂,所以mutations建議只在同步底下呼叫。這時候我們可以使用actions,而也是一個好的習慣不管同步非同步都是先call actions,然後在從actions裡面去commit mutations。
// actions increment: context => { context.commit('increment'); }, decrement: ({commit}) => { commit('decrement'); }, asyncIncrement: ({commit}) => { setTimeout(() => { commit('increment'); }, 1000); }, asyncDecrement: ({commit}) => { setTimeout(() => { commit('decrement'); }, 1000); }
actions 回傳一個callback function帶著context參數 (如increment),我們也可以用ES6的語法省略掉context,直接使用commit。當然 actions也有mapActions可以使用。
import { mapActions } from "vuex"; export default { methods: { ...mapActions (["increment", "decrement"]) } };
mutations, actions帶參數的寫法。payload可以是值也可以是物件
// mutations increment: (state, payload) => { state.counter += payload; } // actions increment: ({commit}, payload) => { commit('increment'); }
// template <button @click="increment(10)">Increment</button> <!-- <button @click="increment({'by':10, 'duration': 1000})">Increment</button> -->
如果要使用two-way-binding如v-model,那原本的computed method可以做以下修改。
computed: { counter(){ get(){ return this.$store.getters.getCounter; }, set(value){
// pretend updateValue is a method inside actions this.$store.dispatch('updateValue', value); } } }
我們可以把new Vuex.Store({…})裡面的getters, mutations…等方法以模塊的方式引入,詳情參考這裡。
如果使用模塊方式引入,那在getters, mutations, actions…等裡面的方法名稱有可能重複,這時候可以使用namespacing的方式解決,詳情參考這裡。
Reference
- https://vuex.vuejs.org/zh/guide/
- https://github.com/vuejs/vuex/releases/tag/v2.1.0