[Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript
With properties we can follow a one-way parent→child flow communication between components. This lesson shows you how you can pass down properties to class based Vue components by using the @Prop decorator from vue-property-decorator.
We’ll also see how we can set types and even default properties on @Prop!
Install:
npm install --save vue-property-decorator
Child:
<template>
<div>
{{fullMessage}}
</div>
</template> <script lang="ts"> import Vue from 'vue'
import {Component, Prop} from 'vue-property-decorator' @Component({})
export default class Child extends Vue {
message: string = "Hello";
@Prop({
type: String,
default: 'Default Value'
}) msg: string; get fullMessage() {
return `${this.message},${this.msg}`;
}
}
</script>
Parent:
<template>
<div class="hello">
<h1 v-colorDirective="{color: 'red', backgroundColor: 'blue'}">{{ message }}</h1>
<button @click="clicked">Click</button>
<ChildComp msg="'What a good day!'"/>
<router-link to="/hello-ts">Hello Ts</router-link>
</div>
</template> <script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import colorDirective from '../color-directive'; import ChildComp from './Child.vue'; @Component({
directives: {
colorDirective
},
components: {
ChildComp
}
})
export default class Hello extends Vue {
message: string = 'Welcome to Your Vue.js App' get fullMessage() {
return `${this.message} from Typescript`
} created() {
console.log('created');
} beforeRouteEnter(to, from, next) {
console.log("Hello: beforeRouteEnter")
next()
} beforeRouteLeave(to, from, next) {
console.log("Hello: beforeRouteLeave")
next()
} clicked() {
console.log('clicked');
}
}
</script>
[Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript的更多相关文章
- [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript
Vue models, v-model, allow us to use two-way data binding, which is useful in some cases such as for ...
- [Vue + TS] Using Route events inside Vue
vue-router introduces new hooks into the component. In this lesson we’ll show you how to use these n ...
- 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践
1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择 ...
- vue + ts Vuex篇
Vuex对Typescript的支持,仍十分薄弱,官方库只是添加了一些.d.ts声明文件,并没有像vue 2.5这样内置支持. 第三方衍生库 vuex-typescript, vuex-ts-deco ...
- vue+ts搭建项目
Tip: 为了避免浪费您的时间,本文符合满足以下条件的同学借鉴参考 1.本文模版不适用于小型项目,两三个页面的也没必要用vue2.对typescript.vue全家桶能够掌握和运用 此次项目模版主要涉 ...
- [Vue + TS] Create Type-Safe Vue Directives in TypeScript
Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...
- Vue源码学习三 ———— Vue构造函数包装
Vue源码学习二 是对Vue的原型对象的包装,最后从Vue的出生文件导出了 Vue这个构造函数 来到 src/core/index.js 代码是: import Vue from './instanc ...
- 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- Vue服务端渲染和Vue浏览器端渲染的性能对比
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
随机推荐
- 安装、配置Vmware Esx Server 3.5视频全过程
Vmware Esx server 的特点是它无需任何操作系统就可在硬件上运行,它的内核是VMware自己开发的VMkernel,可以理解成为Windows系统内核NTOSKRNL.另外它完全依靠Li ...
- OpenCV中Mat数据的访问报错
最近再写一段程序的时候,要访问Mat中的元素.在定义Mat型数据的时候,用 Mat ObjectPoints(48,3,CV_32FC1,0) 对其进行初始化后,用at进行访问时报内存错误. Mat ...
- Win7+MSVC2010+PCL1.7.2
我的配置环境是Win7+MSVC2010+PCL1.7.2. 网上关于点云库配置的文章已经很多,这里不做过多的重复,这里只讲一下自己在配置过程中遇到的问题及一些注意事项. K1: 在用Cmake编译时 ...
- OPENCV(2) —— Basic Structures(二)
Mat OpenCV C++ n-dimensional dense array class The class Mat represents an n-dimensional dense numer ...
- 学习TF:《TensorFlow实战》中文版PDF+源代码
深度学习乃至人工智能正逐渐在FinTech领域发挥巨大的作用,其应用包括自动报告生成.金融智能搜索.量化交易和智能投顾.而TensorFlow为金融业方便地使用深度学习提供了可能.<Tensor ...
- web安全:防范点击劫持的两种方式
防范点击劫持的两种方式 什么点击劫持?最常见的是恶意网站使用 <iframe> 标签把我方的一些含有重要信息类如交易的网页嵌入进去,然后把 iframe 设置透明,用定位的手段的把一些引诱 ...
- Android开发系列(十六):【Android小游戏成语连连看】第二篇
写的晚了,在分工个Z市高中的一个成绩查询的系统,原系统居然是用VB写的,我不得不佩服原本写系统的那位哥们真能耐得住. 明天搭建下SVN就等着先发project款然后開始项目了.想想有工资进账,心里也为 ...
- oracle 数据库批处理文件
文件夹结构 初始化脚本 |----orcl_sql |----init_user.sql |----tab_home.sql |----TAB_USER.sql |----init.bat init. ...
- UML类图中的几种关系总结
UML类图,描写叙述对象和类之间相互关系的方式包含:依赖(Dependency).关联(Association).聚合(Aggregation).组合(Composition).泛化(G ...
- Spring_Learn
IOC 控制反转,或者依赖注入 控制权的转移,应用程序本身不负责依赖对象的创建和维护.而是由外部容器负责创建和维护. DI(依赖注入)是其实现的一种方式 创建对象并且组装对象之间的关系. 1Spr ...