Vue models, v-model, allow us to use two-way data binding, which is useful in some cases such as forms. This lesson shows how to use it by creating a custom checkbox component using the @Model decorator in TypeScript.

When using the v-model, the custom component will receive 'value' prop from the parent component. If we also want to pass ':value' to the custom component, we need to use  '@Model('change') propName'.

v-model will also received 'change' event for components communcation.

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>

[Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript的更多相关文章

  1. [Vue + TS] Create your own Decorators in Vue with TypeScript

    We’ve used @Watch, @Inject and more decorators from vue-property-decorator. In this lesson however w ...

  2. [Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript

    Vue 2.2 introduced a simple dependency injection system, allowing you to use provide and inject in y ...

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

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

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

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

  6. vue+ts搭建项目

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

  7. [Vue 牛刀小试]:第十七章 - 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍

    一.前言 在上一章中,我们开始通过 Vue CLI 去搭建属于自己的前端 Vue 项目模板,就像我们 .NET 程序员在使用 asp.net core 时一样,我们更多的会在框架基础上按照自己的开发习 ...

  8. JS组件系列——又一款MVVM组件:Vue(二:构建自己的Vue组件)

    前言:转眼距离上篇 JS组件系列——又一款MVVM组件:Vue(一:30分钟搞定前端增删改查) 已有好几个月了,今天打算将它捡起来,发现好久不用,Vue相关技术点都生疏不少.经过这几个月的时间,Vue ...

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

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

随机推荐

  1. Visual Studio Code -VS Code

    VS Code 免费开源的编辑器,支持 windows. mac. Linux. 微软出品 官网:https://code.visualstudio.com/ 下载地址:https://code.vi ...

  2. Redis安全与持久化(适合小白阅读)

    前言:Redis的使用越来越重要.以下仅为个人学习的点点记录.仅供参考. 一.简单的redis安全性设置 1. 生产环境的redis最好建议在redis配置文件中设置bind.配置允许指定的ip登陆r ...

  3. [ CodeForces 1063 A ] Oh Those Palindromes

    \(\\\) \(Description\) 给出 \(N\) 个小写字母,将他们排成一个字符串,使得这个字符串里包含的回文串最多. \(N\le 10^5\) \(\\\) \(Solution\) ...

  4. 解决 HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException:

    HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException: The foll ...

  5. UVM基础之-------uvm report机制的使用

    后面的例子我会继续补充: 1. 因为uvm默认定义的message格式比较长,非常不利于debug过程中的分析使用,一般情况下,开始使用uvm,都要利用uvm_report_server重新定义mes ...

  6. 解决TeamViewer的“现在无法捕捉画面。可能由于快速切换或者远程桌面断开或者最小化”问题

    公司项目接近尾声,因为测试的需要,就在客户局域网内部搭建了几台虚拟机做为服务器给客户测试用. 虚拟机环境:win2008R2 原先在客户那边用远程登录虚拟机操作,但考虑到回公司后无法远程登录,所以安装 ...

  7. iframe子页面让父页面跳转 parent.location.href

    if ($roleNum < 9) { echo "<script > parent.location.href='admin_login.php' </script ...

  8. Change the color of a link in an NSMutableAttributedString

    Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...

  9. 牛客多校Round 9

    Solved:1 rank:112 E. Music Game 题解说有个非简化的原题 bzoj4318 #include <bits/stdc++.h> using namespace ...

  10. 【转载】dubbo约束的引入(解决eclipse中报错问题)

    在采用分布式系统架构时,我们会经常使用到阿里巴巴的dubbo的分布式框架. 在相关xml配置了dubbo的约束依赖后,即使能上网eclipse.myeclipse等IDE也是无法识别dubbo的相关约 ...