|
| 1 | +# Components |
| 2 | +This template does not use single file component. Each component consists of 4 files. The reason to do like this is because the editor does not support *.vue files well enough. |
| 3 | + |
| 4 | +## Components structrue |
| 5 | + |
| 6 | +``` |
| 7 | +// for example, we have a components called 'hello'. |
| 8 | +├── hello.scss |
| 9 | +├── hello.ts |
| 10 | +├── hello.vue |
| 11 | +└── index.ts |
| 12 | +``` |
| 13 | + |
| 14 | +## vue file |
| 15 | +As you can see, component template is using .vue file rather than .html file. That is because we can use vue-loader to take advantage of vue single file. |
| 16 | + |
| 17 | +```html |
| 18 | +<!-- vue file --> |
| 19 | +<template> |
| 20 | + <div> |
| 21 | + <h1>Hello {{name}}</h1> |
| 22 | + </div> |
| 23 | +</template> |
| 24 | +<style src="./hello.scss" lang="scss" scoped></style> |
| 25 | +``` |
| 26 | + |
| 27 | +## ts file |
| 28 | +we use [vue-property-decorator](https://github.com/kaorun343/vue-property-decorator) to write typescript component. For the usage, you can read their document. |
| 29 | + |
| 30 | +```typescript |
| 31 | +import Vue from 'components/base' |
| 32 | +import { Component, Prop } from 'vue-property-decorator' |
| 33 | +import template from './hello.vue' |
| 34 | + |
| 35 | +@Component({ |
| 36 | + name: 'tag-hello', |
| 37 | + mixins: [template] // use mixins to mix vue file functions |
| 38 | +}) |
| 39 | +export default class Hello extends Vue { |
| 40 | + // define prop |
| 41 | + @Prop({ default: 'World' }) |
| 42 | + name: string |
| 43 | + |
| 44 | + // computed |
| 45 | + get fullname () { |
| 46 | + return `hello, ${this.name}` |
| 47 | + } |
| 48 | + |
| 49 | + // method |
| 50 | + say () { |
| 51 | + console.log(this.fullname) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +``` |
| 57 | +> Attention:[vue-property-decorator](https://github.com/kaorun343/vue-property-decorator) Component decorator is from [vue-class-component](https://github.com/vuejs/vue-class-component), please read the document. |
| 58 | +
|
| 59 | +## Styling |
| 60 | +Please go to chapter [Styling](./Style.md). |
| 61 | + |
| 62 | +## index.ts |
| 63 | +This is component entry file, so that it will be easy to import other components. |
| 64 | + |
| 65 | +```typescript |
| 66 | +export * from './hello.ts' |
| 67 | +``` |
| 68 | + |
| 69 | +The reason we don't name hello.ts as index.ts is considering editor is always showing file name, thus you will never know which component it belongs to, unless you want to change cli compoent template. |
| 70 | + |
| 71 | +## Import other components or templates |
| 72 | +Import root path is src by default. |
| 73 | + |
| 74 | +```js |
| 75 | +// import 'src/components/tags/hello' |
| 76 | +import Hello from 'components/tags/hello' |
| 77 | +``` |
| 78 | +If you are using vscode,please consider the following configuration: |
| 79 | +```json |
| 80 | +{ |
| 81 | + "editor.quickSuggestions": { |
| 82 | + "other": true, |
| 83 | + "comments": false, |
| 84 | + "strings": true |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | +so when you import module, editor will hint the path. |
0 commit comments