[Vue @Component] Extend Vue Components in TypeScript
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的更多相关文章
- [Vue @Component] Load Vue Async Components
Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...
- [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 ...
- [Vue @Component] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- [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 ...
- [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 ...
- [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 ...
- 前端框架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 ...
- Vue中mixins、extends、extend和components的作用和区别
关于mixins:官方文档: https://cn.vuejs.org/v2/guide/mixins.html 一.components Vue.component是用来注册或获取全局组件的方法,其 ...
- vue.extend与vue.component的区别和联系
一味的闷头开发,却对基础概念缺乏理解,是个大坑... 查阅官网后现对自己的理解记录一下,用于日后复习巩固 Vue.extend({}) 简述:使用vue.extend返回一个子类构造函数,也就是预设部 ...
随机推荐
- 网站开发综合技术 一 JavaScript简介 二JavaScript语法
第1部分 JavaScript简介 1.JavaScript它是个什么东西? 它是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 2.它与Java有什么关系? 没有什么直接联系,java是S ...
- Java 8 (4) Stream 流 - 使用
在本节中将介绍Stream API支持的许多操作,这些操作可以完成更复杂的数据查询,如筛选.切片.映射.查找.匹配和归约.还有一些特殊的流如:数值流.来自文件和数组等多种来源的流. 筛选和切片 1.用 ...
- 专题四:自定义Web浏览器
前言: 前一个专题介绍了自定义的Web服务器,然而向Web服务器发出请求的正是本专题要介绍的Web浏览器,本专题通过简单自定义一个Web浏览器来简单介绍浏览器的工作原理,以及帮助一些初学者揭开浏览器这 ...
- 简单3步,你即可以用上myFocus
Step 1. 在html的标签内引入相关文件 <script type="text/javascript" src="js/myfocus-2.0.0.min.j ...
- arp学习笔记(linux高性能服务编程)
先看看arp的定义吧 现在linux运行这条命令 tcpdump -i eth0:1 -ent '(dst 192.168.5.190 and src 192.168.5.109)or( dst 19 ...
- FCC 基础JavaScript 练习6
1.对象和数组很相似,数组是通过索引来访问和修改数据,对象是通过属性来访问和修改数据的, 对象适合用来存储结构化数据,就和真实世界的对象一模一样,比如一只猫. 任务 创建一个叫做myDog的对象,它里 ...
- impdp and docker install oracleXE
docker oracle https://hub.docker.com/r/sath89/oracle-xe-11g/ docker run -d -p 8080:8080 -p 1521:1521 ...
- 数字图像处理技术在TWaver可视化中的应用
数字图像处理(Digital Image Processing)又称为计算机图像处理,它是指将图像信号转换成数字信号并利用计算机对其进行处理的过程.常用的图像处理方法有图像增强.复原.编码.压缩等,数 ...
- 洛谷——P2018 消息传递
P2018 消息传递 题目描述 巴蜀国的社会等级森严,除了国王之外,每个人均有且只有一个直接上级,当然国王没有上级.如果A是B的上级,B是C的上级,那么A就是C的上级.绝对不会出现这样的关系:A是B的 ...
- tarjan求强连通分量模板
什么是强连通分量? 百度百科 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(stro ...