├── LICENSE ├── README.md └── generate-toc.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ilkwon Sim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vue 實作模式 (vue-patterns) 中文版 2 | 3 | 英文原版:[learn-vuejs](https://github.com/learn-vuejs/vue-patterns) 4 | 繁體中文翻譯:[yoyoys](https://github.com/yoyoys/vue-patterns-cht) 5 | 簡體中文翻譯:[ZYSzys](https://github.com/ZYSzys/vue-patterns-cn) 6 | 7 | 此頁面集結了許多有用的 Vue 實作模式、技術、技巧、以及有幫助的參考連結。 8 | 9 | - [Vue 實作模式 (learn-vuejs) 中文版](#vue-%E5%AF%A6%E4%BD%9C%E6%A8%A1%E5%BC%8F-learn-vuejs-%E4%B8%AD%E6%96%87%E7%89%88) 10 | - [元件宣告](#%E5%85%83%E4%BB%B6%E5%AE%A3%E5%91%8A) 11 | - [單文件組件(Single File Component, SFC) - 最為常見](#%E5%96%AE%E6%96%87%E4%BB%B6%E7%B5%84%E4%BB%B6single-file-component-sfc---%E6%9C%80%E7%82%BA%E5%B8%B8%E8%A6%8B) 12 | - [字串樣板 (String Template) (或是 es6 樣板字面值 (Template Literal)))](#%E5%AD%97%E4%B8%B2%E6%A8%A3%E6%9D%BF-string-template-%E6%88%96%E6%98%AF-es6-%E6%A8%A3%E6%9D%BF%E5%AD%97%E9%9D%A2%E5%80%BC-template-literal) 13 | - [渲染函式 (Render Function)](#%E6%B8%B2%E6%9F%93%E5%87%BD%E5%BC%8F-render-function) 14 | - [JSX](#jsx) 15 | - [vue-class-component (使用 es6 classes)](#vue-class-component-%E4%BD%BF%E7%94%A8-es6-classes) 16 | - [參考連結](#%E5%8F%83%E8%80%83%E9%80%A3%E7%B5%90) 17 | - [元件之間的溝通(Component Communication)](#%E5%85%83%E4%BB%B6%E4%B9%8B%E9%96%93%E7%9A%84%E6%BA%9D%E9%80%9Acomponent-communication) 18 | - [屬性與事件(Props and Events)](#%E5%B1%AC%E6%80%A7%E8%88%87%E4%BA%8B%E4%BB%B6props-and-events) 19 | - [元件事件處理方法(Component Events Handling)](#%E5%85%83%E4%BB%B6%E4%BA%8B%E4%BB%B6%E8%99%95%E7%90%86%E6%96%B9%E6%B3%95component-events-handling) 20 | - [元件條件渲染 (Component Conditional Rendering)](#%E5%85%83%E4%BB%B6%E6%A2%9D%E4%BB%B6%E6%B8%B2%E6%9F%93-component-conditional-rendering) 21 | - [指令 (Directives) (`v-if` / `v-else` / `v-else-if` / `v-show`)](#%E6%8C%87%E4%BB%A4-directives-v-if--v-else--v-else-if--v-show) 22 | - [JSX](#jsx) 23 | - [動態元件](#%E5%8B%95%E6%85%8B%E5%85%83%E4%BB%B6) 24 | - [元件組合](#%E5%85%83%E4%BB%B6%E7%B5%84%E5%90%88) 25 | - [基本組合 (Basic Composition)](#%E5%9F%BA%E6%9C%AC%E7%B5%84%E5%90%88-basic-composition) 26 | - [繼承 (Extends)](#%E7%B9%BC%E6%89%BF-extends) 27 | - [混入 (Mixins)](#%E6%B7%B7%E5%85%A5-mixins) 28 | - [預設插槽 (Slots (Default))](#%E9%A0%90%E8%A8%AD%E6%8F%92%E6%A7%BD-slots-default) 29 | - [具名插槽(Named Slots)](#%E5%85%B7%E5%90%8D%E6%8F%92%E6%A7%BDnamed-slots) 30 | - [作用域插槽 (Scoped Slots)](#%E4%BD%9C%E7%94%A8%E5%9F%9F%E6%8F%92%E6%A7%BD-scoped-slots) 31 | - [渲染屬性 (Render Props)](#%E6%B8%B2%E6%9F%93%E5%B1%AC%E6%80%A7-render-props) 32 | - [參數傳遞 (Passing Props)](#%E5%8F%83%E6%95%B8%E5%82%B3%E9%81%9E-passing-props) 33 | - [高優先元件 (Higher Order Component, HOC)](#%E9%AB%98%E5%84%AA%E5%85%88%E5%85%83%E4%BB%B6-higher-order-component-hoc) 34 | - [相依注入 (Dependency injection)](#%E7%9B%B8%E4%BE%9D%E6%B3%A8%E5%85%A5-dependency-injection) 35 | - [**提供** 與 **注入** (Provide / Inject)](#%E6%8F%90%E4%BE%9B-%E8%88%87-%E6%B3%A8%E5%85%A5-provide--inject) 36 | - [注入裝飾器模式 (@Provide / @Inject Decorator)](#%E6%B3%A8%E5%85%A5%E8%A3%9D%E9%A3%BE%E5%99%A8%E6%A8%A1%E5%BC%8F-provide--inject-decorator) 37 | - [錯誤處理 (Handling Errors)](#%E9%8C%AF%E8%AA%A4%E8%99%95%E7%90%86-handling-errors) 38 | - [`errorCaptured` 事件](#errorcaptured-%E4%BA%8B%E4%BB%B6) 39 | - [生產力小技巧](#%E7%94%9F%E7%94%A2%E5%8A%9B%E5%B0%8F%E6%8A%80%E5%B7%A7) 40 | - [有用的連結](#%E6%9C%89%E7%94%A8%E7%9A%84%E9%80%A3%E7%B5%90) 41 | - [重構技巧](#%E9%87%8D%E6%A7%8B%E6%8A%80%E5%B7%A7) 42 | - [狀態管理](#%E7%8B%80%E6%85%8B%E7%AE%A1%E7%90%86) 43 | - [Vuex](#vuex) 44 | - [Mobx](#mobx) 45 | - [不須渲染的元件 (Renderless Component)](#%E4%B8%8D%E9%A0%88%E6%B8%B2%E6%9F%93%E7%9A%84%E5%85%83%E4%BB%B6-renderless-component) 46 | - [目錄結構](#%E7%9B%AE%E9%8C%84%E7%B5%90%E6%A7%8B) 47 | - [小技巧](#%E5%B0%8F%E6%8A%80%E5%B7%A7) 48 | - [路由(Router)](#%E8%B7%AF%E7%94%B1Router) 49 | - [不良示範 (反模式)](#%E4%B8%8D%E8%89%AF%E7%A4%BA%E7%AF%84-%E5%8F%8D%E6%A8%A1%E5%BC%8F) 50 | - [影片與音訊課程](#%E5%BD%B1%E7%89%87%E8%88%87%E9%9F%B3%E8%A8%8A%E8%AA%B2%E7%A8%8B) 51 | - [專案範例](#%E5%B0%88%E6%A1%88%E7%AF%84%E4%BE%8B) 52 | - [付費課程](#%E4%BB%98%E8%B2%BB%E8%AA%B2%E7%A8%8B) 53 | - [Typescript](#typescript) 54 | - [Flowtype](#flowtype) 55 | - [GraphQL](#graphql) 56 | - [其他資訊](#%E5%85%B6%E4%BB%96%E8%B3%87%E8%A8%8A) 57 | 58 | ## 元件宣告 59 | 60 | ### 單文件組件(Single File Component, SFC) - 最為常見 61 | 62 | ```html 63 | 68 | 69 | 83 | 84 | 89 | ``` 90 | 91 | ### 字串樣板 (String Template) (或是 es6 樣板字面值 (Template Literal)) 92 | 93 | ```js 94 | Vue.component('my-btn', { 95 | template: ` 96 | 99 | `, 100 | data() { 101 | return { 102 | text: 'Click me', 103 | }; 104 | }, 105 | methods: { 106 | handleClick() { 107 | console.log('clicked'); 108 | }, 109 | }, 110 | }); 111 | ``` 112 | 113 | ### 渲染函式 (Render Function) 114 | 115 | ```js 116 | Vue.component('my-btn', { 117 | data() { 118 | return { 119 | text: 'Click me', 120 | }; 121 | }, 122 | methods: { 123 | handleClick() { 124 | console.log('clicked'); 125 | }, 126 | }, 127 | render(h) { 128 | return h('button', { 129 | attrs: { 130 | class: 'btn-primary' 131 | }, 132 | on: { 133 | click: this.handleClick, 134 | }, 135 | }); 136 | }, 137 | }); 138 | ``` 139 | 140 | ### JSX 141 | 142 | ```jsx 143 | Vue.component('my-btn', { 144 | data() { 145 | return { 146 | text: 'Click me', 147 | }; 148 | }, 149 | methods: { 150 | handleClick() { 151 | console.log('clicked'); 152 | }, 153 | }, 154 | render() { 155 | return ( 156 | 159 | ); 160 | }, 161 | }); 162 | ``` 163 | 164 | ### [vue-class-component](https://github.com/vuejs/vue-class-component) (使用 es6 classes) 165 | 166 | ```html 167 | 172 | 173 | 186 | 187 | 192 | ``` 193 | 194 | #### 參考連結 195 | 196 | * [7 Ways To Define A Component Template in VueJS](https://medium.com/js-dojo/7-ways-to-define-a-component-template-in-vuejs-c04e0c72900d) 197 | 198 | ## 元件之間的溝通(Component Communication) 199 | 200 | ### 屬性與事件(Props and Events) 201 | 202 | 基本上,Vue 元件是依照單向資料流傳遞資料,這就是屬性進、事件出(props down, event up)[請參考官方說明](https://vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow))。 203 | (譯註:也有稱作props in, emit out。) 204 | 屬性(props)是唯讀的資料,所以不可能從子元件內部修改他的值,若是外部傳入的屬性變化了,子元件將會自動重繪(因為屬性是反應性資料)。 205 | 子元件只能將事件發送到父代,藉此父代可以修改資料(`data`) ,這個資料也對應到子元件的屬性(`props`)。 206 | 207 | ```html 208 | 211 | 212 | 220 | ``` 221 | 222 | ```html 223 | 226 | 227 | 243 | ``` 244 | 245 | #### 參考連結: 246 | 247 | * [Vue.js Component Communication Patterns](https://alligator.io/vuejs/component-communication/) 248 | * [Creating Custom Inputs With Vue.js](https://www.smashingmagazine.com/2017/08/creating-custom-inputs-vue-js/) 249 | * [Vue Sibling Component Communication](https://vegibit.com/vue-sibling-component-communication/) 250 | * [Managing State in Vue.js](https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87) 251 | 252 | ## 元件事件處理方法(Component Events Handling) 253 | 254 | #### 參考連結: 255 | 256 | * [Leveraging Vue events to reduce prop declarations](https://itnext.io/leveraging-vue-events-to-reduce-prop-declarations-e38f5dce2aaf) 257 | * [Vue.js Component Hooks as Events](https://alligator.io/vuejs/component-event-hooks/) 258 | * [Creating a Global Event Bus with Vue.js](https://alligator.io/vuejs/global-event-bus/) 259 | * [Vue.js Event Bus + Promises](https://medium.com/@jesusgalvan/vue-js-event-bus-promises-f83e73a81d72) 260 | 261 | ## 元件條件渲染 (Component Conditional Rendering) 262 | 263 | ### 指令 (Directives) (`v-if` / `v-else` / `v-else-if` / `v-show`) 264 | 265 | `v-if` 266 | 267 | ```html 268 |

只在 v-if 值為 true 時渲染

269 | ``` 270 | 271 | `v-if` 與 `v-else` 272 | 273 | ```html 274 |

只在 v-if 值為 true 時渲染

275 |

只在 v-if 值為 false 時渲染

276 | ``` 277 | 278 | `v-else-if` 279 | 280 | ```html 281 |
只在 `type` 等於 `A` 時渲染
282 |
只在 `type` 等於 `B` 時渲染
283 |
只在 `type` 等於 `C` 時渲染
284 |
只在 `type` 不等於>fmf `A` 或 `B` 或 `C` 時渲染
285 | ``` 286 | 287 | `v-show` 288 | 289 | ```html 290 |

永遠都會渲染,但是只在 `v-show` 值為 true 時顯示

291 | ``` 292 | 293 | 如果你需要同時在多個元素上面做條件式渲染,你可以在 `