@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>

@Model  数据双向绑定

Checkbox:

<template>
<div>
<input type="checkbox" :id="id" :checked=checked @change="changed"/> {{label}}
</div>
</template> <script lang="ts">
import Vue from 'vue'
import { Component, Prop, Model } from 'vue-property-decorator'
@Component
export default class MyCheckbox extends Vue {
@Prop() label: string
@Prop() id: string @Prop()
@Model('change') checked: boolean changed(ev) {
this.$emit('change', ev.target.checked)
}
}
</script>

Parent Component:

<template>
<div>
<MyCheckbox :label="checkbox.label" :id="checkbox.id" v-model="checkbox.checked"/> {{JSON.stringify(checkbox)}}
</div>
</template>
<script lang="ts"> import Vue from 'vue'
import {Component} from 'vue-property-decorator'
import MyCheckbox from './MyCheckBox.vue' @Component({
components: {
MyCheckbox
}
})
export default class HelloTs extends Vue { checkbox = {
label: 'Fancy checkbox',
id: 'checkbox-id',
checked: true
}
}
</script>

@Watch  监听数据变化

<template>
<div class="hello">
<button @click="clicked">Click</button> {{sum.acum}}
</div>
</template> <script lang="ts">
import Vue from 'vue'
import {Component, Watch} from 'vue-property-decorator' @Component({
})
export default class Hello extends Vue { sum = {
acum: 0
}
@Watch('sum', {deep: true})
watchCount(newVal, oldVal) {
console.log("newVal", newVal, "oldVal", oldVal)
} clicked() {
this.sum.acum++;
}
}
</script>

@Provide  提供  /  @Inject  注入

当您希望从父组件到子组件提供一些服务或数据时,您可以使用@Provide和@Inject。

Parent component:

<template>
<div class="hello">
<ChildComp :msg="'What a good day!'"/>
</div>
</template> <script lang="ts">
import Vue from 'vue'
import {Component, Provide} from 'vue-property-decorator' import ChildComp from './Child.vue'; @Component({
})
export default class Hello extends Vue { @Provide('users')
users = [
{
name: 'test',
id: 0
}
] }
</script>

Child:

<template>
<div>
{{users}}
</div>
</template> <script lang="ts"> import Vue from 'vue'
import {Component, Inject} from 'vue-property-decorator' @Component({})
export default class Child extends Vue {
message: string = "Hello"; @Inject('users') users;
}
</script>

在 TypeScript 中创建 自己的修饰器

定义一个修饰器:

const Log = (msg) => {
return createDecorator((component, key) => {
console.log("#Component", component);
console.log("#Key", key); //log
console.log("#Msg", msg); //App
})
}

使用:

@Log('fullMessage get called')
get fullMessage() {
return `${this.message} from Typescript`
}

输出:

#Component Object {directives: Object, components: Object, name: "Hello", methods: Object, computed: Object…}
#Key fullMessage
#Msg fullMessage get called

.

vue-property-decorator 提供 OO 的风格 Vue Component 方便类型声明的更多相关文章

  1. Property 'validate' does not exist on type 'Element | Element[] | Vue | Vue[]'. Property 'valid...

    使用vue-cli 3.0+Element-ui时候,调用form表单校验时候出现的问题是: Property 'validate' does not exist on type 'Element | ...

  2. 前端MVC Vue2学习总结(二)——Vue的实例、生命周期与Vue脚手架(vue-cli)

    一.Vue的实例 1.1.创建一个 Vue 的实例 每个 Vue 应用都是通过 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 ...

  3. Vue01 Vue介绍、Vue使用、Vue实例的创建、数据绑定、Vue实例的生命周期、差值与表达式、指令与事件、语法糖

    1 Vue介绍 1.1 官方介绍 vue是一个简单小巧的渐进式的技术栈,它提供了Web开发中常用的高级功能:视图和数据的解耦.组件的服用.路由.状态管理.虚拟DOM 说明:简单小巧 -> 压缩后 ...

  4. 前端笔记之Vue(一)初识SPA和Vue&webpack配置和vue安装&指令

    一.单页面应用(SPA) 1.1 C/S到B/S页面架构的转变 C/S:客户端/服务器(Client/Server)架构的软件. C/S 软件的特点: ① 从window桌面双击打开 ② 更新的时候会 ...

  5. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  6. Vue学习【第一篇】:Vue初识与指令

    什么是Vue 什么是Vue Vue.js是一个渐进式JavaScript框架它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 渐进式:vue从小到控制页面中的一个变量 ...

  7. Vue入门系列(三)之Vue列表渲染及条件渲染实战

    Vue官网: https://cn.vuejs.org/v2/guide/forms.html#基础用法 [入门系列] (一)  http://www.cnblogs.com/gdsblog/p/78 ...

  8. [vue]vue v-on事件绑定(原生修饰符+vue自带事件修饰符)

    preventDefault阻止默认行为和stopPropagation终止传递 event.preventDefault() 链接本来点了可以跳转, 如果注册preventDefault事件,则点了 ...

  9. 【vue】index.html main.js app.vue index.js怎么结合的? 怎么打包的?搜集的信息

    转载:https://blog.csdn.net/yudiandemingzi/article/details/80247137 怎么结合的: 一.启动项目 第一步:cmd进入项目文件里,运行npm ...

随机推荐

  1. coon's patch

    作者:桂. 时间:2018-05-23  06:11:54 链接:https://www.cnblogs.com/xingshansi/p/9070761.html 前言 早晨突然想到计算机模型的各种 ...

  2. 使用protobuf编译onnx.proto过程中的一些问题总结

    使用git clone下载protobuf的源代码,然后git checkout到branch2.7.0: 编译protobuf,先在代码顶层目录执行./configure,然后执行make,成功后执 ...

  3. Kubernetes 1.12公布:Kubelet TLS Bootstrap与Azure虚拟机规模集(VMSS)迎来通用版本号

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/82880341 https: ...

  4. 关于Java 软件工程师应该知道或掌握的技术栈

    鄙人星云,今天突然想写这么一篇需要持续更新的文章,主要目的用于总结当前最流行的技术和工具,方便自己也方便他人. 更新时间:2018-10-23 09:26:19 码农职业路径图 码农入门职业路径图 J ...

  5. Visual Studio 2015 msvsmon.exe crashed when c++ debugging with x64

    Completely uninstalling Astrill fixed the issue but this solution is not what I want. Astrill suppor ...

  6. git忽略.idan目录

    git rm -r --cached .idea git add . git commit -m '忽略idea' git pull git push

  7. Linux的rp_filter与策略路由

    Linux的rp_filter用于实现反向过滤技术,也即uRPF,它验证反向数据包的流向,以避免伪装IP攻击,但是它和Linux的策略路由却很容易发生冲突,其本质原因在于,uRPF技术强制规定了一个反 ...

  8. Python3 File

    open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open() ...

  9. 翻译下 golang package time

    # 关于 `package time` 个人体会:"wall clock" 可以理解为就是实际的时钟,而 "monotonic clock" 则是程序内部的时钟 ...

  10. dedecms wap 上一篇 下一篇 链接出错

    打开 \include\arc.archives.class.php 文件 大约在839 行,查找  $mlink = 'view.php?aid='.$preRow['id'];        修改 ...