2021年8月6日 星期五
線上程式碼編輯器 StackBlitz
2021年8月5日 星期四
安裝 Caddy 發生 ca-certificates 錯誤解決方式
按照「Ubuntu 安裝 web server Caddy 2」遇到的安裝問題:
sudo apt update
執行第二行 update 後會跳出錯誤訊息:
"Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: XX.XXX.XX.XXX XXX] Reading package lists... Done W: https://apt.fury.io/caddy/InRelease: No system certificates available. Try installing ca-certificates.
代表需要先安裝 ca-certificates
安裝後重新再一次指令 sudo apt update ,就不會出現錯誤了。
參考資料:
https://caddy.community/t/solved-failed-to-install-caddy-with-official-auto-installation-command/9877
2021年2月21日 星期日
【升版指南】Vue 3 宣告事件 emits
Vue 3 新增 emits ,可以宣告需要傳遞到上層的事件名稱,而在 Vue2 時不需要宣告:
<template>
<div>
<p v-text="message" />
<button v-on:click="$emit('accepted')">OK</button>
</div>
</template>
<script>
export default {
props: ['message']
}
</script>
在 Vue3 要在 emits 加上 accepted,寫法與 props 宣告的方式相似 :
<template>
<div>
<p v-text="message" />
<button v-on:click="$emit('accepted')">
OK
</button>
</div>
</template>
<script>
export default {
props: ['message'],
emits: ['accepted'],
}
</script>
雖然這個變動不大,但如果元件內忘記宣告,上層會收到兩次的事件觸發,造成非預期性的事件產生。如同 props ,事件傳遞的資料可以加上驗證 (validator)。假設有個輸入訊息的 message 會透過 Button 點擊送出,期望送出的文字至少要10個字:
<template>
<div>
<input v-model="message">
<button v-on:click="$emit('confirm', message)">
OK
</button>
</div>
</template>
<script>
export default {
emits: {
confirm: message => message.length > 10,
},
data() {
return {
message: undefined,
};
},
}
</script>
參考資料:https://v3.vuejs.org/guide/migration/emits-option.html
【升版指南】Vue 3 移除 $listeners 後如何跨元件傳遞事件
在 Vue3 移除 $listeners ,全部改到 $attrs 裡,因此在 Vue2 裡可能會有這種情況:
<template>
<label>
<input
type="text"
v-bind="$attrs"
v-on="$listeners">
</label>
</template>
<script>
export default {
inheritAttrs: false
}
</script>
在 Vue3 ,因為 $listeners 不存在,所有的綁定只要使用 $attrs :
<template>
<label>
<input
type="text"
v-bind="$attrs">
</label>
</template>
<script>
export default {
inheritAttrs: false
}
</script>
假設只需要傳遞特定的事件,而非所有的事件,舉例要傳遞的事件是 update-name ,Vue 2 的寫法利用 $listeners 會是:
<template>
<my-element
:name="$attrs.name"
@update-name="$listeners['update-name']" />
</template>
在 Vue3 裡需要加上 on 作為前綴,上層元件就能夠收到事件 update-name。
<template>
<my-element
:name="$attrs.name"
@update-name="$attrs['onUpdateName']" />
</template>
參考資料:https://v3.vuejs.org/guide/migration/listeners-removed.html
2020年11月25日 星期三
Ubuntu 安裝 web server Caddy 2
安裝
按照指令安裝 Caddy 2
sudo apt update
sudo apt install caddy
安裝完成後,執行 caddy 應該會跑出相關的指令提示
常用指令
執行 caddy
等同於 caddy run,在背景程式運作
將 Caddyfile 轉成 json config
caddy adapt --config [PATH_TO_FILE]
重新載入 caddy,可使新的設定生效
關閉 caddy
2020年11月2日 星期一
[Vue.js] vue-i18n 實現多語系
快速使用
假設你已經安裝 Vue cli 3.x,你可以直接到專案裡執行:
vue add i18n
接著會詢問你預設的語言、備用語系(fallbackLocale)及字典檔的目錄,所謂的備用語系指的是當使用者選擇語言的字典檔有缺時,會採用哪個語言的資料替代。
若按照預設值執行的話,可以確認一下專案是否出現目錄 /[Your Project]/src/locales/ ,裡面有個 en.json 英文的字典檔。
這個方法相關的設定已經自動完成,可以直接在 template 上使用:
{{ $t('message') }}
安裝及設定
Step 1: 安裝套件
npm install vue-i18n
Step 2: 新建目錄及字典檔
mkdir locales新增目錄 /[Your Project]/src/locales/ ,加入第一個字典檔 en.json 。
{
"hello": "Hello!"
}
接著 index.js
import en from './en';
export const messages = {
en,
};
你也可以在 locales/ 下繼續新增其他語系
Step 3: 設定
新增 i18n.js 設定 locale 預設語言及 fallbackLocale 備用語言等,messages 是所有語系的字典檔。
import Vue from 'vue';
import VueI18n from 'vue-i18n';
import {messages} from './locales';
Vue.use(VueI18n);
export default new VueI18n({
locale: 'en', // set locale
fallbackLocale: 'en',
messages, // set locale messages
});
將設定加入 main.js
import Vue from 'vue'
import App from './App.vue'
import i18n from './i18n'
Vue.config.productionTip = false
new Vue({
i18n,
render: h => h(App),
}).$mount('#app')
試著在 template 上使用:
{{ $t('hello') }}
上述的檔案完成後,專案的架構如下列所示:
./src/ ├── App.vue ├── i18n.js ├── locales │ ├── en.json │ └── index.js └── main.js
更多的安裝資訊可以參考[文件]
進階使用
切換語系
在 root 更改設定值$i18n.locale
備用語系
可依據各語言設定,可接受傳入字串、陣列、物件等fallbackLocale
2020年10月16日 星期五
Ubuntu 20 安裝及建立 Angular 專案
sudo apt update sudo apt install build-essential libssl-dev curl https://raw.githubusercontent.com/creationix/nvm/v0.36.0/install.sh | sh source ~/.profile nvm install v12.19.0
安裝 Angular CLI 套件
npm install -g @angular/cli
建立專案
新增一個 my-app 的專案,執行指令後會詢問是否安裝 Angular routing 及樣式語言(stylesheet),不選擇的話可以直接按enter使用預設值。ng new my-app執行需要一點時間,完成後會在目前的目錄中出現一個 my-app/ 的資料夾。
執行
cd my-app ng serve
請用瀏覽器開啟頁面
# run `ng serve --open` to open your browser automatically
http://localhost:4200/
相關文章:
2020年9月7日 星期一
Asciidoctor 專案使用 Travis CI 自動部屬程式範例
一、新增 .travis.yml
先將專案編譯成 HTML 等,並將相關的檔案放到新建的目錄 /publc 。script:
- bundle exec rake book:build
- mkdir public
- cp progit.html public/index.html
- cp progit.{pdf,epub} public/
- cp -r images/ public/
接著是部屬的部分,目錄 /publc 就是我們要放在網頁上的資料,develop 則是我們目前的分支名稱:
deploy:
provider: pages
skip_cleanup: true
github_token: $GITHUB_API_TOKEN
local_dir: ./public
on:
branch: develop
.travis.yml 完整程式碼請參考[這裡],或到官網參考更多的設定參數。
二、設定 Github Token
請參考 [這裡] 第二步驟,按照方法在 Travis CI 新增環境變數 GITHUB_API_TOKEN 。三、更新
2020年7月21日 星期二
[Vue.js] $ref 和 directive 操作 DOM 的兩種方式及範例
一、自訂索引名 ($refs)
自訂索引名有點像是 id 一樣,假設在元件上設定 ref myBlock,就可以用 this.$refs.myBlock 取得 DOM 元素。<template>
<input
v-model.number="heightModel"
type="number">
<div
ref="myBlock"
class="border border-danger">
Block
</div>
</template>
<script>
export default {
data() {
return {
height: 50,
};
},
computed: {
heightModel: {
get() {
return this.height;
},
set(height) {
this.height = height;
this.changeHeight(height);
},
},
},
mounted() {
if (this.height) {
this.changeHeight(this.height);
}
},
methods: {
changeHeight(height) {
this.$refs.myBlock.style.height = `${height}px`;
},
}
}
</script>
二、自定義指令 (custom directive)
常用的 v-model 就是一種指令(directive),你也可以自訂指令方便全域使用。若定義 focus,在 template 上就使用 v-focus:// Register a global custom directive called `v-focus`
Vue.directive('focus', {
// When the bound element is inserted into the DOM...
inserted: function (el) {
// Focus the element
el.focus()
}
})<template>
<input
v-model.number="height"
type="number">
<div
v-height="height"
class="border border-danger">
Block
</div>
</template>
<script>
export default {
directives: {
height: (el, binding) => {
el.style.height = `${binding.value}px`;
},
},
data() {
return {
height: 50,
};
},
}
</script>See the Pen Vue.js Reference ID VS Custom Directives by chenuin (@chenuin) on CodePen.
2020年6月25日 星期四
Travis CI 自動 Jekyll 專案部屬 Github Pages 完整範例
一、新增編譯腳本
mkdir script vim script/cibuildscript/cibuild 內容如下(參考檔案):
#!/usr/bin/env bash set -e # halt script on error JEKYLL_ENV=production bundle exec jekyll build # bundle exec htmlproofer ./_site
二、新增 Travis CI 設定檔
.travis.yml 內容如下(參考檔案):language: ruby
rvm:
- 2.6
script: ./script/cibuild
before_script:
- chmod +x ./script/cibuild
deploy:
provider: pages
skip_cleanup: true
github_token: $GITHUB_TOKEN
repo: chenuin/chenuin.github.io
local_dir: ./_site
target_branch: master
on:
branch: master
TOKEN 設定(參考)
- 開啟頁面 https://github.com/settings/tokens/new
- 填寫備註(可以自行決定),並選擇 public_repo
- 送出
三、上傳
git add . git commit -m 'init commit' git push origin master


