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. pyDes 实现 Python 版的 DES 对称加密/解密--转

    https://my.oschina.net/leejun2005/blog/586451 手头有个 Java 版的 DES 加密/解密程序,最近想着将其 Python 重构下,方便后续脚本解析,捣鼓 ...

  2. Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解

    步骤1.破解包下载地址:https://gitee.com/pengzhile/MyBatisCodeHelper-Pro-Crack/releases 步骤2.下载:Intellij IDEA  p ...

  3. LN : leetcode 513 Find Bottom Left Tree Value

    lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...

  4. LR接口测试---Java Vuser之jdbc查询(调试前)

    在eclipse下编写好的代码: import lrapi.lr; import java.sql.Connection; import java.sql.DriverManager; import ...

  5. DIV水平 垂直居中CSS

    /*实现一.原理:要让div等块级元素水平和垂直居中,必需知道该div等块级元素的宽度和高度,然后设置位置为绝对位置,距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度 ...

  6. Spring与Struts2集成开发

    Struts2和Spring都是不错的开源框架,Spring与Struts2集成开发,把二者结合在一起使用,开发效果更佳,效率杠杠的.下面介绍一下如何将Spring与Struts2集成在一起开发.分七 ...

  7. idea之快速查看类所在jar包

  8. 暴力搜索+散列--P1008 三连击

    题目描述 将1,2, ⋯,9共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行,每行3 ...

  9. Window下的———TOMCAT环境的配置

    1. 先去官方网站下载需要的猫(tomcat) http://tomcat.apache.org/ 2.下载好包,然后解压出来,放在你需要的位置上 3.去到配环境变量的地方,进行相应的环境配置     ...

  10. Java 十二周总结