Vue Typescript @Prop

news/2024/7/9 23:57:46 标签: vue
语法:
	@Prop(options: (PropOptions | Constructor[] | Constructor) = {})
	
参数说明:
	@Prop装饰器接收一个参数,这个参数可以有三种写法:

	Constructor
		例如String,Number,Boolean等,指定 prop 的类型;
		
	Constructor[]
		指定 prop 的可选类型;
		
	PropOptions
		可以使用以下选项:type,default,required,validator。
<!--
 * @FilePath: \hello-typescript\src\components\Child.vue
-->
<template>
    <div style="border: 1px solid black;padding: 2rem;">
        <h1>Child Component</h1>
        <span>{{pMsg}}</span><br/>
        <!-- <span>{{prMsg}}</span><br/> -->
        <span>{{child}}</span><br/>
        <span>{{childA}}</span><br/>
        <span>{{childB}}</span><br/>
        <span>{{childC}}</span><br/>
        <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

    // props 数据范畴 !表示不需要构造器进行数据初始化
    // 必要参数 数据类型 字符串 或 数字 只读 单向同步数据,如果利用prop反向同步数据会报错
    @Prop({ type: [String, Number], required: true }) readonly child!: string | number
    // 非必须参数
    @Prop(Number) readonly childA!: number
    // 非必传参数,拥有默认值
    @Prop({ default: 'default value' }) readonly childB!: string
    // 非必传 需要构建器初始化 仅一个参数时默认为type
    @Prop([String, Boolean]) readonly childC: string | boolean | undefined

    // 转发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="content" :childA="childA" :childB="childB" :childC="childC"></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 = '初始化内容'
  childA = 1
  childB = 'B'
  childC = true

  // 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)
    // 非响应式数据
    this.msg = val
    // 修改prop
    this.childB = val
  }
}
</script>


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

相关文章

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实战案例…

HTML基本介绍及标签应用

HTML基本介绍及标签应用1、初识HTML1.1 前端技术栈1.2 html概述1.3 html基本结构1.4 html基本内容2、网页基本标签2.1 标题标签2.2 段落标签2.3 换行标签2.4 水平线标签2.5 字体样式标签2.5.1 粗体2.5.2 斜体2.6 注释和特殊符号3、列表标签3.1 有序列表(ol)3.2 无序列表(ul)3.3…