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. node-rsa加密,java解密调试

    用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常.但是用node加密玩的java解密不了.原因:node默认的是 DEFAULT_ENCRYPTION_SCHEM ...

  2. SQL数据库基础知识——抽象类

    抽象类,只为继承而出现,不定义具体的内容,只规定该有哪些东西:一般抽象类中只放置抽象方法,只规定了返回类型和参数:比如: 人 - 有吃饭,睡觉方法: 男人 - 继承人抽象类,必须实现吃饭,睡觉的方法主 ...

  3. request获取请求参数

    /** * 方式1 */ Enumeration<?> enu=request.getParameterNames(); while(enu.hasMoreElements()){ Str ...

  4. Django--1、MTV及基本应用

    web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,以避免重复造轮子. 所有的Web应用,本质上是一个socket服务 ...

  5. Android RxJava2 浅析

    原文地址:http://blog.csdn.net/maplejaw_/article/details/52442065 Observable 在RxJava1.x中,最熟悉的莫过于Observabl ...

  6. View Programming Guide for iOS

    https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/Wi ...

  7. libevent reference Mannual II--library

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html The Libevent Reference Manual: Preliminari ...

  8. vs2012+ winform+.net4.0发布如何在xp上运行

    今天在英文版vs2013打包发布4.0(非4.0 client)的winform时,遇到了在xp上无法运行的情况,.net framework 4.0在xp上已安装.在打包前,winform工程,即菜 ...

  9. POJ3624 0-1背包(dp+滚动数组)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47440   Accepted: 20178 ...

  10. RequestMapping_请求参数&请求头

    params和headers支持简单的表达式: --param1:表示请求必须包含名为param1的请求参数. --!param1:表示请求不能包含名为param1的请求参数. --param1 != ...