Vue本身一個很好用的特性是可以利用Vue的元件(Component)去拼成一個頁面。將頁面拆成元件的好處是這些元件可以被重覆的使用在其他頁面上。 一個父元件裡面可以有許多子元件,而子元件裡面當然可以有更多的子元件。要做到一個父/子之間的互動,傳遞參數必然少不了。單純的垂直參數傳遞可以透過props和emit事件來達成。 父元件透過props傳遞參數給子元件,子元件則靠emit事件回給父元件監聽。看似單純,但其實有許多有趣的眉角在裏頭。
顯示完整資訊Bootstrap4 Set Global font-size筆記
嘗試用Bootstrap4的sass去改全站預設基底的字型大小, 雖然bootstrap的scss裡面有一個$font-size-base, 在最後會被用在_reboot.scss的body裡面,但個人還是偏愛設定在html的標籤裡。
第一次設定很單純的在html裡面直接加了font-size: 0.875rem; (1rem=16px)。因為bootstrap裡面的單位大都是rem,所以自然改了這個root參數,全站相對單位應該都會等比例縮放。但意外地發現,並非想像那樣。table裡的字型竟然沒變,依然是16px而非我設定的14px。開了console看一下發現它吃到的是browser agent default stylesheet,也就是bootstrap裡面的reboot應該是沒有全部reset。後來套用一下Eric A. Meyer大神的Reset CSS其中一塊,修改如下,順利解決了我的問題。
顯示完整資訊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
npm & vue cli 新手筆記
安裝套件
npm install [-g|--save|--save-dev] {package_name} -g 全域 --save 專案本身 (含prod &dev) --save-dev 專案本身 (dev only) npm install -g @vue/cli ( vue-cli 3) npm install -g vue-cli (vue-cli 2)
下載modules (需下這個指令才會把package download下來)
npm install
使用vue-cli2建立專案
vue init {template_name} {project_name} template name: webpack, webpack-simple, browserify, browserify-simple, simple
使用vue-cli3建立專案
vue create {project_name}
使用vue-cli3 UI建立專案
vue ui
在vue-cli3底下使用vue init方式建立專案
vue install -g @vue/cli-init
VueJS 基礎語法練習
在jsfiddle上做一些VueJS基礎語法的練習。 雖然git也可以達到這樣的版本效果 ,覺得拿jsfiddle類似這樣的工具做前端語言練習真的滿方便的。
顯示完整資訊