|
1 | 1 | # h5-vue
|
2 | 2 |
|
3 |
| -用 vue 写 h5 项目的基本结构与依赖整理,封装常用工具,快速开发 h5 的脚手架。<br> |
| 3 | +用 vue 写 h5 项目的基本结构与依赖整理,封装常用工具,快速开发 h5 的脚手架。 |
4 | 4 |
|
5 | 5 | [TOC]
|
6 | 6 |
|
|
78 | 78 |
|
79 | 79 | ## 附录
|
80 | 80 |
|
81 |
| -### Vue + TypeScript 文档 |
82 |
| - |
83 |
| -官方文档:[vue-class-component](https://github.com/vuejs/vue-class-component) |
84 |
| - |
85 |
| -- `methods` 可以直接声明为类成员方法。 |
86 |
| -- 可以将计算属性声明为属性访问器。 |
87 |
| -- 初始化 `data` 可以声明为类属性。 |
88 |
| -- `data`, `render` 与 Vue 所有声明周期钩子都可以声明为成员方法。 |
89 |
| -- 对于其他类型,将它们传递给装饰器函数。 |
90 |
| - |
91 |
| -``` |
92 |
| -<script> |
93 |
| -import Vue from 'vue' |
94 |
| -import Component from 'vue-class-component' |
95 |
| -
|
96 |
| -@Component({ |
97 |
| - props: { |
98 |
| - propMessage: String |
99 |
| - } |
100 |
| -}) |
101 |
| -export default class App extends Vue { |
102 |
| - // initial data |
103 |
| - msg = 123 |
104 |
| -
|
105 |
| - // use prop values for initial data |
106 |
| - helloMsg = 'Hello, ' + this.propMessage |
107 |
| -
|
108 |
| - // lifecycle hook |
109 |
| - mounted () { |
110 |
| - this.greet() |
111 |
| - } |
112 |
| -
|
113 |
| - // computed |
114 |
| - get computedMsg () { |
115 |
| - return 'computed ' + this.msg |
116 |
| - } |
117 |
| -
|
118 |
| - // method |
119 |
| - greet () { |
120 |
| - alert('greeting: ' + this.msg) |
121 |
| - } |
122 |
| -} |
123 |
| -</script> |
124 |
| -``` |
125 |
| - |
126 |
| -#### 其他类型 - Mixins |
127 |
| - |
128 |
| -``` |
129 |
| -// mixin.js |
130 |
| -import Vue from 'vue' |
131 |
| -import Component from 'vue-class-component' |
132 |
| -
|
133 |
| -// You can declare a mixin as the same style as components. |
134 |
| -@Component |
135 |
| -export default class MyMixin extends Vue { |
136 |
| - mixinValue = 'Hello' |
137 |
| -} |
138 |
| -
|
139 |
| -// app.js |
140 |
| -import Component, { mixins } from 'vue-class-component' |
141 |
| -import MyMixin from './mixin.js' |
142 |
| -
|
143 |
| -// Use `mixins` helper function instead of `Vue`. |
144 |
| -// `mixins` can receive any number of arguments. |
145 |
| -@Component |
146 |
| -export class MyComp extends mixins(MyMixin) { |
147 |
| - created () { |
148 |
| - console.log(this.mixinValue) // -> Hello |
149 |
| - } |
150 |
| -} |
151 |
| -``` |
152 |
| - |
153 |
| -#### 类属性使用注意事项 |
154 |
| - |
155 |
| -- `this` 指向问题,不要使用箭头函数定义类成员方法: |
156 |
| - |
157 |
| -``` |
158 |
| -@Component |
159 |
| -class MyComp extends Vue { |
160 |
| - foo = 123 |
161 |
| -
|
162 |
| - bar = () => { |
163 |
| - // Does not update the expected property. |
164 |
| - // `this` value is not a Vue instance in fact. |
165 |
| - this.foo = 456 |
166 |
| - } |
167 |
| -} |
168 |
| -
|
169 |
| -@Component |
170 |
| -class MyComp extends Vue { |
171 |
| - foo = 123 |
172 |
| -
|
173 |
| - bar () { |
174 |
| - // Correctly update the expected property. |
175 |
| - this.foo = 456 |
176 |
| - } |
177 |
| -} |
178 |
| -``` |
179 |
| - |
180 |
| -- `undefined` 不是响应式的 |
181 |
| - |
182 |
| -``` |
183 |
| -@Component |
184 |
| -class MyComp extends Vue { |
185 |
| - // Will not be reactive |
186 |
| - foo = undefined |
187 |
| -
|
188 |
| - // Will be reactive |
189 |
| - bar = null |
190 |
| -
|
191 |
| - data () { |
192 |
| - return { |
193 |
| - // Will be reactive |
194 |
| - baz: undefined |
195 |
| - } |
196 |
| - } |
197 |
| -} |
198 |
| -``` |
199 |
| - |
200 |
| -### 装饰器扩展指南 |
201 |
| - |
202 |
| -官方文档:[vue-property-decorator](https://github.com/kaorun343/vue-property-decorator) |
203 |
| - |
204 |
| -- @Prop(options: (PropOptions | Constructor[] | Constructor) = {}) |
205 |
| - |
206 |
| -``` |
207 |
| -import { Vue, Component, Prop } from 'vue-property-decorator' |
208 |
| -
|
209 |
| -@Component |
210 |
| -export default class YourComponent extends Vue { |
211 |
| - @Prop(Number) propA!: number |
212 |
| - @Prop({ default: 'default value' }) propB!: string |
213 |
| - @Prop([String, Boolean]) propC: string | boolean |
214 |
| -} |
215 |
| -``` |
216 |
| - |
217 |
| -相当于 |
218 |
| - |
219 |
| -``` |
220 |
| -export default { |
221 |
| - props: { |
222 |
| - propA: { |
223 |
| - type: Number |
224 |
| - }, |
225 |
| - propB: { |
226 |
| - default: 'default value' |
227 |
| - }, |
228 |
| - propC: { |
229 |
| - type: [String, Boolean] |
230 |
| - }, |
231 |
| - } |
232 |
| -} |
233 |
| -``` |
234 |
| - |
235 |
| -- @Model(event?: string, options: (PropOptions | Constructor[] | Constructor) = {}) |
236 |
| - |
237 |
| -``` |
238 |
| -import { Vue, Component, Model } from 'vue-property-decorator' |
239 |
| -
|
240 |
| -@Component |
241 |
| -export default class YourComponent extends Vue { |
242 |
| - @Model('change', { type: Boolean }) checked!: boolean |
243 |
| -} |
244 |
| -``` |
245 |
| - |
246 |
| -相当于: |
247 |
| - |
248 |
| -``` |
249 |
| -export default { |
250 |
| - model: { |
251 |
| - prop: 'checked', |
252 |
| - event: 'change' |
253 |
| - }, |
254 |
| - props: { |
255 |
| - checked: { |
256 |
| - type: Boolean |
257 |
| - }, |
258 |
| - }, |
259 |
| -} |
260 |
| -``` |
261 |
| - |
262 |
| -- @Watch(path: string, options: WatchOptions = {}) |
263 |
| - |
264 |
| -``` |
265 |
| -import { Vue, Component, Watch } from 'vue-property-decorator' |
266 |
| -
|
267 |
| -@Component |
268 |
| -export default class YourComponent extends Vue { |
269 |
| - @Watch('child') |
270 |
| - onChildChanged(val: string, oldVal: string) { } |
271 |
| -
|
272 |
| - @Watch('person', { immediate: true, deep: true }) |
273 |
| - onPersonChanged(val: Person, oldVal: Person) { } |
274 |
| -} |
275 |
| -``` |
276 |
| - |
277 |
| -相当于: |
278 |
| - |
279 |
| -``` |
280 |
| -export default { |
281 |
| - watch: { |
282 |
| - 'child': { |
283 |
| - handler: 'onChildChanged', |
284 |
| - immediate: false, |
285 |
| - deep: false |
286 |
| - }, |
287 |
| - 'person': { |
288 |
| - handler: 'onPersonChanged', |
289 |
| - immediate: true, |
290 |
| - deep: true |
291 |
| - } |
292 |
| - }, |
293 |
| - methods: { |
294 |
| - onChildChanged(val, oldVal) { }, |
295 |
| - onPersonChanged(val, oldVal) { } |
296 |
| - } |
297 |
| -} |
298 |
| -``` |
299 |
| - |
300 |
| -- @Emit(event?: string) |
301 |
| - |
302 |
| -``` |
303 |
| -import { Vue, Component, Emit } from 'vue-property-decorator' |
304 |
| -
|
305 |
| -@Component |
306 |
| -export default class YourComponent extends Vue { |
307 |
| - count = 0 |
308 |
| -
|
309 |
| - @Emit() |
310 |
| - addToCount(n: number) { |
311 |
| - this.count += n |
312 |
| - } |
313 |
| -
|
314 |
| - @Emit('reset') |
315 |
| - resetCount() { |
316 |
| - this.count = 0 |
317 |
| - } |
318 |
| -
|
319 |
| - @Emit() |
320 |
| - returnValue() { |
321 |
| - return 10 |
322 |
| - } |
323 |
| -
|
324 |
| - @Emit() |
325 |
| - promise() { |
326 |
| - return new Promise(resolve => { |
327 |
| - setTimeout(() => { |
328 |
| - resolve(20) |
329 |
| - }, 0) |
330 |
| - }) |
331 |
| - } |
332 |
| -} |
333 |
| -``` |
334 |
| - |
335 |
| -相当于: |
336 |
| - |
337 |
| -``` |
338 |
| -export default { |
339 |
| - data() { |
340 |
| - return { |
341 |
| - count: 0 |
342 |
| - } |
343 |
| - }, |
344 |
| - methods: { |
345 |
| - addToCount(n) { |
346 |
| - this.count += n |
347 |
| - this.$emit('add-to-count', n) |
348 |
| - }, |
349 |
| - resetCount() { |
350 |
| - this.count = 0 |
351 |
| - this.$emit('reset') |
352 |
| - }, |
353 |
| - returnValue() { |
354 |
| - this.$emit('return-value', 10) |
355 |
| - }, |
356 |
| - promise() { |
357 |
| - const promise = new Promise(resolve => { |
358 |
| - setTimeout(() => { |
359 |
| - resolve(20) |
360 |
| - }, 0) |
361 |
| - }) |
362 |
| -
|
363 |
| - promise.then(value => { |
364 |
| - this.$emit('promise', value) |
365 |
| - }) |
366 |
| - } |
367 |
| - } |
368 |
| -} |
369 |
| -``` |
370 |
| - |
371 |
| -- @Provide(key?: string | symbol) / @Inject(options?: { from?: InjectKey, default?: any } | InjectKey) |
372 |
| - |
373 |
| -``` |
374 |
| -import { Component, Inject, Provide, Vue } from 'vue-property-decorator' |
375 |
| -
|
376 |
| -const symbol = Symbol('baz') |
377 |
| -
|
378 |
| -@Component |
379 |
| -export class MyComponent extends Vue { |
380 |
| -@Inject() foo!: string |
381 |
| -@Inject('bar') bar!: string |
382 |
| -@Inject({ from: 'optional', default: 'default' }) optional!: string |
383 |
| -@Inject(symbol) baz!: string |
384 |
| -
|
385 |
| -
|
386 |
| -@Provide() foo = 'foo' |
387 |
| -@Provide('bar') baz = 'bar' |
388 |
| -} |
389 |
| -``` |
390 |
| - |
391 |
| -相当于: |
392 |
| - |
393 |
| -``` |
394 |
| -const symbol = Symbol('baz') |
395 |
| -
|
396 |
| -export const MyComponent = Vue.extend({ |
397 |
| -
|
398 |
| - inject: { |
399 |
| - foo: 'foo', |
400 |
| - bar: 'bar', |
401 |
| - 'optional': { from: 'optional', default: 'default' }, |
402 |
| - [symbol]: symbol |
403 |
| - }, |
404 |
| - data () { |
405 |
| - return { |
406 |
| - foo: 'foo', |
407 |
| - baz: 'bar' |
408 |
| - } |
409 |
| - }, |
410 |
| - provide () { |
411 |
| - return { |
412 |
| - foo: this.foo, |
413 |
| - bar: this.baz |
414 |
| - } |
415 |
| - } |
416 |
| -}) |
417 |
| -``` |
| 81 | +- [Vue + TypeScript 的基础用法](./docs/vue-typescript.md) |
0 commit comments