This lesson shows how you can extend and reuse logic in Vue components using TypeScript inheritance. It will take you through extending a component, its properties and methods, and how hooks are triggered along the inheritance tree.

We can define a Parent.ts file, which only contains the logic without any template:

import { Component, Vue } from 'vue-property-decorator';

@Component
export default class Parent extends Vue {
created() {
console.log("Parent is created")
} click() {
console.log("Parent is clicked")
} parentClicked() {
console.log("Parent is clicked")
}
}

Then we can extends this Parent Class from a vue component:

<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>{{ fullMessage }}</h2>
<button @click="click">Click</button>
<button @click="parentClicked">Parent Click</button>
</div>
</template> <script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Parent from './Parent'; @Component
export default class HelloWorld extends Parent {
@Prop() private msg!: string; // replace computed props
get fullMessage() {
return `${this.msg} should be fullmessage from a getter`
} created() {
console.log("Component is created")
} click() {
alert('Should replace what used to be defined in methods objects')
}
}
</script>

Once we extends from Parent, HelloWorld Component can inherit its Parent class's methods and props.

For example:

Will call parentClicked method from Parent Class from HelloWorld Component.

<!-- HelloWorld.vue -->
<button @click="parentClicked">Parent Click</button>

If we don't define 'click' method in HelloWolrd component, it will using Parent's click() method.

[Vue @Component] Extend Vue Components in TypeScript的更多相关文章

  1. [Vue @Component] Load Vue Async Components

    Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...

  2. [Vue + TS] Write a Vue Component as a Class in TypeScript

    Starter app: https://github.com/alexjoverm/Vue-Typescript-Starter Writing Vue components as plain ob ...

  3. [Vue @Component] Dynamic Vue.js Components with the component element

    You can dynamically switch between components in a template by using the reserved <component>  ...

  4. [Vue @Component] Write Vue Functional Components Inline

    Vue's functional components are small and flexible enough to be declared inside of .vue file next to ...

  5. [Vue @Component] Simplify Vue Components with vue-class-component

    While traditional Vue components require a data function which returns an object and a method object ...

  6. [Vue @Component] Pass Vue Render Functions as Props for Powerful Patterns

    Render functions open up a world of customization and control by using pure JavaScript rather than V ...

  7. 前端框架vue.js系列(9):Vue.extend、Vue.component与new Vue

    前端框架vue.js系列(9):Vue.extend.Vue.component与new Vue 本文链接:https://blog.csdn.net/zeping891103/article/det ...

  8. Vue中mixins、extends、extend和components的作用和区别

    关于mixins:官方文档: https://cn.vuejs.org/v2/guide/mixins.html 一.components Vue.component是用来注册或获取全局组件的方法,其 ...

  9. vue.extend与vue.component的区别和联系

    一味的闷头开发,却对基础概念缺乏理解,是个大坑... 查阅官网后现对自己的理解记录一下,用于日后复习巩固 Vue.extend({}) 简述:使用vue.extend返回一个子类构造函数,也就是预设部 ...

随机推荐

  1. c# Queue实现生产者(Producer)消费者(Consumer)模式

    我们在开发过程中经常会遇到需要从一个地方不断获取数据然后又需要交给另一个线程对数据进行二次加工的情况,这种场景适合使用生产者-消费者模式. Demo展示 //中间的容器 public static c ...

  2. cocos2d-x android 环境部署

    1.下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.下载 and ...

  3. Android Studio3.0打包APK的时候 报错:

    Error:Execution failed for task ':app:transformDexWithDexForRelease'.> com.android.build.api.tran ...

  4. ionic3带参数返回原来页面

    最近用ionic3+angular4做项目.我遇到了个问题,我返回原来页面时一般都会调用this.navCtrl.pop()方法,但这个方法不能携带参数.怎么办? 可以写个回调方法. 我在a页面定义个 ...

  5. git上手简洁手册

    下载安装git 创建文件夹:learngit 用Git CMD进入文件夹: cd learngit 用Git CMD初始化git: git init 创建文件:新建一个文件在learngit文件夹下, ...

  6. Redis系列(三)--消息队列、排行榜等

    Redis命令执行生命周期: 发送命令--->排队(单线程)--->执行命令--->返回结果 慢查询: 只是针对命令执行阶段 慢查询日志通过一个固定长度的FIFO queue,这个q ...

  7. vue组件---动态组件之多标签页面

    首先看下效果图 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  8. 关于DOS-BOX的使用方法

    将MASM文件夹里的全部文件拷贝到一个目录下,比如E:\masm下,然后将这个目录挂着为DOSBox的一个盘符下,挂载命令为 Mount c e:\masm 切换到E盘 然后编译,运行

  9. Xcode5编译ffmpeg

    命令行安装FFmpeg:git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg(或:到https://github.com/gabriel/ffmpeg ...

  10. php第三十节课

    文件操作 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...