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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践

    1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择  ...

  4. vue + ts Vuex篇

    Vuex对Typescript的支持,仍十分薄弱,官方库只是添加了一些.d.ts声明文件,并没有像vue 2.5这样内置支持. 第三方衍生库 vuex-typescript, vuex-ts-deco ...

  5. vue+ts搭建项目

    Tip: 为了避免浪费您的时间,本文符合满足以下条件的同学借鉴参考 1.本文模版不适用于小型项目,两三个页面的也没必要用vue2.对typescript.vue全家桶能够掌握和运用 此次项目模版主要涉 ...

  6. [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 ...

  7. Vue源码学习三 ———— Vue构造函数包装

    Vue源码学习二 是对Vue的原型对象的包装,最后从Vue的出生文件导出了 Vue这个构造函数 来到 src/core/index.js 代码是: import Vue from './instanc ...

  8. 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)

    Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...

  9. Vue服务端渲染和Vue浏览器端渲染的性能对比

    Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...

随机推荐

  1. PHP从数组中删除元素的方法

    PHP从数组中删除元素的方法 本篇文章主要介绍了PHP从数组中删除元素的四种方法实例 删除一个元素,且保持原有索引不变 使用 unset 函数,示例如下: 1 2 3 4 5 <?php   $ ...

  2. 7. java操作MongoDB,采用_id查询

    转自:https://www.2cto.com/database/201704/633262.html mongodb命令行_id查询方法 直接用ObjectId() db.getCollection ...

  3. vue父子间通信案列三($emit和prop用法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. c# 导出excel格式xlsx

    string sb="";//sql字符串 AttachmentConfigSection configSection = ConfigurationManager.GetSect ...

  5. 【RHEL7/CentOS7网络配置】

    目录 网卡配置文件 查网卡信息 测试网络是否正常 使用 nmtui 命令配置网络 使用 nm-connection-editor 工具配置网络 修改回6.x版本的网卡名 Rhel/CentOS网络配置 ...

  6. 使用Linux遇到的一些问题和解决方案

    1.如何在重装系统或换机器以后继续使用以前用过的thunderbird邮件 执行命令thunderbird -ProfileManager以后会打开一个配置用户界面. 在里面添加一个新的配置,然后选择 ...

  7. 小贝_redis高级应用-安全性

    redis高级应用-安全性 一.为什么redis须要安全性 二.设置redis验证password 三.验证   一.为什么redis须要安全性 1.redis作为数据的存储介质.假设无法保证redi ...

  8. python-openpyxl安装

    今天在安装openpyxl的时候,一直提示错误,后来才发现仅仅安装它还不够,还需要其他两个库的支持1.安装jdcal2.安装et_xmlfile这两个库安装的方法,都是直接在命令行下面,进入库文件se ...

  9. ElasticSearch 架构图

    ElasticSearch 架构图 从下往上来分析ElasticSearch 架构图 Gateway代表ElasticSearch索引的持久化存储方式. 在Gateway中,ElasticSearch ...

  10. java(内部类)

    内部类: 一个类定义在另外一个类的内部就称作为内部类. 内部类的类别: 1.成员内部类: 2.局部内部类: 1.成员内部类: 成员内部类的访问方式: 方式一:在成员内部类的外侧提供一个方法创建内部类的 ...