header、footer、sidebar 都會很常重複使用,content 是會變化的,用不同元件來插入。網頁最外層就是根元件,也就是createApp 建立的元件,稱為「根元件」。
元件註冊分為兩種:「全域註冊」和「區域註冊」。
const app = Vue.createApp({
data() {
return {
text: '外部元件文字'
};
},
});
app.component("alert", {
data() {
return {
text: '內部文字'
};
},
template: `<div class="alert alert-primary" role="alert">
{{ text }}
</div>`
});
app.mount("#app");
元件上的名稱可作為標籤的名稱,即可渲染至畫面上
<alert></alert>
元件程式碼要先註冊在其中一個區塊內,只有在該區塊中才能使用,其他不行。
const content = {
data() {
return {
text: '這是 content 子元件'
}
},
template: `<div class="content">{{ text }}</div>`
}
const app = createApp({
data() {
return {
text: '123'
}
},
components: {
content,
}
})
把元件獨立成一個檔案,透過 ESModule 的方式,來進行匯入。
export default {
data() {
return {
text: '外部匯入的元件'
}
},
template: `<div class="alert alert-primary" role="alert">
{{ text }}
</div>`
}
import alert4 from './component-alert.js'
const app = Vue.createApp({
data() {
return {
text: '外部元件文字'
}
},
components: {
alert4
}
});
app.mount("#app");S
Learn More →
Vue
六角 2022 Vue 直播班
元件
大公司通常都會有不同的部門,財務管財務,行銷管行銷,業務管業務,雖然彼此有關聯,有往來,但基本上都是獨立運作,有什麼問題就去相對應的部門中做調整。 程式也是一樣,規模一龐大,就需要採取「關注點分離」的原則,把東西都拆分清楚,讓整體更容易管理和維護。 關注點分離 (SOC) 是指程式開發時,必須重視的一種「設計原則」。將程式拆成不同功能層或模組,讓每個區塊有各自的關注點,才不會全部混在一起,像一團毛線球剪不斷理還亂。 以 Vue 為例 ---------------- HTML ----------------------------------------- JS (資料、渲染、生命週期)-------------
Mar 24, 2023template 加入 template 屬性,用反引號框住樣板內容,元件名稱直接作為標籤名稱。 app.component("alert", { template: `<div class="alert alert-primary" role="alert">範例一</div>` }); x-template 建立 script,加入 type="text/x-template" id="樣板名稱",script 中放入樣板內容,component 中的 template 補上 '#樣板名稱'。 <script type="text/x-template" id="alert-template">
Feb 7, 2023增加程式碼的可複用性
Feb 7, 2023Youtube Link To sum up: 各種「學習」方式改為英文 (看書/學管理/學泡咖啡/學瑜珈/投資...等) 各種「娛樂」方式改為英文 (看書/聽故事/podcast/聽歌/Youtube/追劇/看劇評/冥想引導...等) 看英文相關的社群媒體/影片/App 跟外國人交流 (朋友聊天、同事溝通) 外國網站購物 (逛網拍、英文聯絡客服) 各種要做/看的事情改為英文 (手機介面/搜尋靈感/物品貼英文標籤/寫日記/Google 語音助理/讀Google 評價/寫代辦事項...等)
Feb 6, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up