首先在ts中属性、模板名字等都有个横杠和大写的转换的,比如父组件中是 <color-drawer>
,则子组件的名字是ColorDrawer.vue
其次在ts中全部都需要用装饰器说明下面的内容是啥(是组件、还是信号发射器、还是prop属性等),所以必须引入装饰器:import { Component, Prop, Vue, Emit, Mixins, Watch } from 'vue-property-decorator'
接下来就是在父组件中引入子组件,并且注册组件,引入组件和js中是一样的。
import ColorDrawer from './moudle/ColorDrawer'
注册则需要在component装饰器下面,注意这里有两层component,漏掉一个会导致错误TS2769: No overload matches this call.
并且注意这个@Component 是装饰器,用来装饰 下面的class ColorFather的,装饰器和被装饰类之间不能插入其他语句,否则会报错Decorators are not valid here
//这是父组件
@Component({
components: {
ColorDrawer
}
})
export default class C