Vue Typescript @Model

news/2024/7/10 0:07:36 标签: vue
定制prop和event
用作双向数据绑定
默认情况下, 一个组件上的v-model会:
	将 value用作 prop
	将 input用作 event
			
语法:	
	model: {prop?: string, event?: string} 
<!--
 * @FilePath: \hello-typescript\src\components\Child.vue
-->
<template>
    <div style="border: 1px solid black;padding: 2rem;">
        <h1>Child Component</h1>
        <input type="text" :value="value" @input="changed" placeholder="please write something..."/>
    </div>
</template>
<script lang="ts">
// InjectReactive
import { Component, Model, Vue, Emit, Inject, Prop } from 'vue-property-decorator'

@Component
export default class Chlid extends Vue {
    // data 数据范畴
    // model 数据绑定 也属于data 数据范畴,属于本地成员变量数据
    // 获取父组件关于input输入框中 value变量的绑定值 model 绑定为动态双向绑定
    @Model('input') value!: boolean

    // 转发input事件,动作名称叫input,parent中绑定名称也应交做input
    @Emit('input')
    // eslint对any类型警告,可以在package.json中关闭 也可以对指定地方关闭
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    changed (event: any) {
      return event.target.value
    }
}
</script>

<!--
 * @FilePath: \hello-typescript\src\views\Parent.vue
-->
<template>
    <div>
      <h1>Parent</h1>
      <Child v-model="content" @input="childChange"></Child><br/>
      <p>
        <b>
          Child 与 Parent 绑定的内容 => {{content}}
        </b>
      </p>
    </div>
</template>
<script lang="ts">
// ProvideReactive
import { Component, Vue, Provide } from 'vue-property-decorator'
import Child from '@/components/Child.vue'
@Component({
  components: {
    Child
  }
})
export default class Parent extends Vue {
  // data 范畴的数据
  // 一个组件只会有一个 v-model
  content = '初始化内容'

  // methods
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private childChange (val: any, event: any) {
    window.console.log('val', val)
    window.console.log('event', event)
  }
}
</script>


http://www.niftyadmin.cn/n/1162485.html

相关文章

Vue Typescript @Prop

语法&#xff1a;Prop(options: (PropOptions | Constructor[] | Constructor) {})参数说明&#xff1a;Prop装饰器接收一个参数&#xff0c;这个参数可以有三种写法&#xff1a;Constructor例如String&#xff0c;Number&#xff0c;Boolean等&#xff0c;指定 prop 的类型&a…

vue + typescript 引入Vant-UI

安装 vant npm install vant --save1. 全局引入在 main.ts 中全局引入import vant/lib/vant-css/index.cssVue.use(Vant)2. 按需引入 两种按需引入方法&#xff0c;都可以实现按照需求引入&#xff0c;推荐第二种第一种&#xff1a;步骤一&#xff1a;在src下定义 plugins 目录…

vue 项目中常用的三种数据存储

vue 项目中常用的三种数据存储 1. Cookies 存储 介绍&#xff1a; 老技术、指定有效期、不能跨域&#xff08;浏览器&#xff0c;网站&#xff0c;路径&#xff09; cookie会跟随http协议发往后台&#xff0c;cookie最好处于服务器环境中使用 只能…

JS 序列化 与 反序列化

序列化 定义&#xff1a; 序列化是把对象转换成有序字节流&#xff0c;以便在网络上传输或者保存在本地文件中。 序列化机制的核心作用就是对象状态的保存与重建。 本质上讲&#xff0c;序列化就是把实体对象状态按照一定的格式写入到有序字节流 …

nodejs + express 发布vue项目

利用nodejs express 实现vue项目的快速发布 安装环境&#xff1a;nodejs : https://www.runoob.com/nodejs/nodejs-install-setup.html 菜鸟教程 很详细全面express: npm install express --save -gexpress-generator: npm install express-generator --save -g 创建项目…

windows下 Nginx 发布 Vue项目

安装环境&#xff1a;nginx: https://nginx.org/en/download.html 根据环境下载对应版本在指定目录解压即可使用。 打包Vue项目&#xff1a;npm run build 打包文件在dist目录下 部署项目&#xff1a;将dist目录下打包文件迁移至nginx html目录下 修改配置&#xff1a;根据实…

Markdown最基础快捷键学习--一篇文章掌握markdown简单书写

Markdown学习 标题 三级标题 四级标题 五级标题 六级标题 字体 粗体hello&#xff0c;world 斜体hello&#xff0c;world 斜体加粗hello&#xff0c;world 删除线hello&#xff0c;world 引用 引用 分割线 图片 本地图片 &#xff01;网络图片 网络图片 超链接 …

Vue的基础语法和简单入门--针对后端学前端技术

Vue的基础语法和简单入门Vuevue基础引用循环和判断判断循环事件数据的双向绑定vue组件网络通信-axios计算属性内容分发自定义事件Vue-cli主要功能环境安装验证安装vue-cli创建vue项目创建项目初始化包初始化并运行Webpack安装测试安装webpack demovue-router安装demovue实战案例…