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. ImmutableJS

    引用大神的一句话:(具体是谁自己问度娘) Shared mutable state is the root of all evil(共享的可变状态是万恶之源) -- Pete Hunt   JavaS ...

  2. 个人软件过程(psp)需求分析

    个人软件过程(psp)需求分析 1.  引言 1.1  背景 开发项目进度计划不准确,延期经常出现,甚至无法给出一个比较准确的延迟时间,给市场推广带来很大麻烦. 2.  任务概述 2.1 目标 PSP ...

  3. Spartan6系列之器件引脚功能详述

    1.   Spartan-6系列封装概述 Spartan-6系列具有低成本.省空间的封装形式,能使用户引脚密度最大化.所有Spartan-6 LX器件之间的引脚分配是兼容的,所有Spartan-6 L ...

  4. 云服务IaaS,PaaS,SaaS

    IaaS:基础设施服务,Infrastructure-as-a-service PaaS:平台服务,Platform-as-a-service SaaS:软件服务,Software-as-a-serv ...

  5. C++ 泛型程序设计与STL模板库(1)---泛型程序设计简介及STL简介与结构

    泛型程序设计的基本概念 编写不依赖于具体数据类型的程序 将算法从特定的数据结构中抽象出来,成为通用的 C++的模板为泛型程序设计奠定了关键的基础 术语:概念 用来界定具备一定功能的数据类型.例如: 将 ...

  6. intellij idea console 乱码

    修改文件 位置:{用户目录}\{iedea对应版本}\{idea or idea64}.vmoptions 比如我要修改我的配置文件 C:\Users\kkblf\.IntelliJIdea2017. ...

  7. CAD二次开发控件,dwg控件,网页DWG控件,手机浏览编辑DWG控件

    梦想绘图插件5.2(MxDraw5.2) 是国内最强,最专业的CAD开发插件(控件),不需要AutoCAD就能独立运行. 控件使用VC 2010开发,具有30万行代码规模,最早从2007年第一个版本完 ...

  8. php中 如何找到session 的保存位置

    [前言] 刚刚想测试FQ操作,需要删除session,这里记录分享下 [主体] (1)想要查看session保存的目录,需要先找到 php.ini配置文件 (2)在php.ini文件中查找 sessi ...

  9. python多进程和多线程编程

    17 多线程和多进程并发 The modules described in this chapter provide support for concurrent execution of code. ...

  10. 洛谷——P3018 [USACO11MAR]树装饰Tree Decoration

    P3018 [USACO11MAR]树装饰Tree Decoration 比较水的一道树上模拟水题,更新每个点的价值为以这个点为根的子树中的价值最小值,同时更新以每个节点为根的$sum$值,即以这个节 ...