vue+typescript+vue-property-decorator中v-model联动总结

最近的项目使用的技术栈为 vue + typescrit + vue-property-decorator

可以不使用 vue模版组件,通过 'vue-property-decorator'插件,写出oo风格的组件

其中的 v-model 和 .vue 模版有些区别
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'

@Component
export default class Son extends Vue {
    // @Prop ({default: ''}) str! :string
    @Prop () value!: string
    // @Prop ({type: Array}) value!: any[]
    @Prop ({type: Array}) data!: any[]
    @Prop ({type: Number}) num!: number
    // value 是 联动的 是 ‘v-model’ 最好value 结合 model ,
    // 此处是value 结合 tempStr,也可达到效果,但是prop 一定是 value
    // str  不是联动的
    // data 是联动的
    // model = this.value
    tempData = this.data
    tempStr: string = this.value
    tempNum = this.num

    @Watch ('value')
    onValueChange() {
        // this.model = this.value
        this.tempStr = this.value
        console.log('son value changes....')
    }
    @Watch('tempStr')
    onModelChange(val) {
        console.log('son model changes...')
        // this.$emit('input',this.tempStr)
    }
    @Watch ('data') 
    onDataChange() {
        // 此处 数据会联动, 但不会进入到这里
        console.log('data changes')
        this.tempData = this.data
    }
    @Watch ('str') 
    onStrChange() {
        console.log('whosd changes')
        this.tempStr = this.str
    }
    @Watch ('tempStr') 
    onTempArr() {
        console.log('tempArr changes...')
    }
    
    render () {
        return (
            <div style={{width: '100px', height: '50px', border: '1px solid #999'}}>
                <input type="text" v-model={this.tempStr}/>
                <br/>
                <input type="text" v-model={this.tempNum}/>
                <ul>
                    {this.tempData.map(item=>{
                        return (
                            <li>
                                <input v-model={item.age} />
                            </li>
                        )
                    })}
                </ul>
            </div>
        )
    }

}

Prop是 对象时,数据通过 v-model时,改变时,会修改data和Prop,并且会修改 父组件传入的对象,不通过 v-model,通过prop 也可以达到同样效果,命名不要求时 value 和 model
Prop是 字符串,布尔值,数字时,数据通过 v-model时,改变时,会修改data但是不会修改Prop,也不会修改 父组件传入的值
Prop 中是value时,data最好命名为 model,但命名为其他也可以达到效果,
需要在:注意⚠️ 如果value是对象类型时,是不需要下面 $emit()

@Watch('model')
    onModelChange(val) {
        this.$emit('input',this.model)
    }
在父组件中调用
import Son from './Son.tsx'
....
render () {
        return (
            <div style={{width: '200px', height: '100px', border: '1px solid #333'}}>
                 <Son v-model={this.str}  data={this.arrList} ></Son>
            </div>
        )
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,072评论 0 29
  • vue概述 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来声明...
    li4065阅读 7,266评论 0 25
  • 以下内容是我在学习和研究Vue时,对Vue的特性、重点和注意事项的提取、精练和总结,可以做为Vue特性的字典; 1...
    科研者阅读 14,122评论 3 24
  • 主要还是自己看的,所有内容来自官方文档。 介绍 Vue.js 是什么 Vue (读音 /vjuː/,类似于 vie...
    Leonzai阅读 3,373评论 0 25
  • 此文基于官方文档,里面部分例子有改动,加上了一些自己的理解 什么是组件? 组件(Component)是 Vue.j...
    陆志均阅读 3,846评论 5 14