Vue2.0+ ts(TypeScript)常用装饰器

装饰器
一、前言
二、装饰器
三、常用装饰器使用方式
1、@Component 和 @Prop 装饰器
2、@Emit 装饰器
3、@Model 装饰器
4、@Watch 装饰器
5、@Provide 装饰器 和 @Inject 装饰器
6、@PropSync
一、前言
在构建完项目之后,上一篇构建地址 ,打开src/components/HelloWorld.vue,将会发现写法已大有不同,如图

看到上面代码引入了vue-property-decorator 它是依赖于 vue-class-component

Install

        npm i -S vue-property-decorator
        //如果是直接用vuecli+ts构建的话,已经有依赖

二、装饰器
1、vue-property-decorator 装饰器有几个装饰器和1个功能(Mixin):
- @Prop
- @PropSync
- @model
- @Watch
- @Provide
- @inject
- @ProvideReactive
- @InjectReactive
- @Emit
- @Ref
- @Component (由vue-class-component提供)
- Mixins (由vue-class-component提供的名为Mixins的助手函数)

2、vuex-class的装饰器
- @State
- @Getter
- @Action
- @Mutation

三、常用装饰器使用方式
1、@Component 和 @Prop 装饰器
@Component 装饰器是用以声明子组件
@Prop 装饰器是用以接收来自父组件的数据

1)父组件

<template>
  <div class="home">               
    <HelloWorld msg="Welcome to Your Vue.js + TypeScript App"/>
  </div>
</template>            
<script lang="ts">
    import { Component, Vue } from 'vue-property-decorator';
    import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src                
    @Component({
      components: {HelloWorld,},
    })
    export default class Home extends Vue {}
</script>

2)子组件

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;    // ! 表示确定msg有值
}
</script>

//----------------------------------------------------
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;    // ! 表示确定msg有值  
  @Prop(Number) readonly propA: number | undefined
  @Prop({ default: 'default value' }) readonly propB!: string
  @Prop([String, Boolean]) readonly propC: string | boolean | undefined
}
// 上面的类似于☟
export default {
  props: {
    msg: {
      type: String,
      default: '',
    },
    propA: {
      type: Number,
    },
    propB: {
     type: String,
      default: 'default value',
    },
    propC: {
      type: [String, Boolean],
    },
  },
}

2、@Emit 装饰器
@Emit 装饰器是用以子组件触发父组件的自定义事件。

import { Vue, Component, Emit } from 'vue-property-decorator'
@Component
export default class YourComponent extends Vue {
//子组件里面的定义
  count = 0
  @Emit()
  addToCount(n: number) {
    this.count += n
  }

  @Emit('reset')
  resetCount() {
    this.count = 0
  }

  @Emit()
  returnValue() {
    return 10
  }

  @Emit()
  onInputChange(e) {
    return e.target.value
  }

  @Emit()
  promise() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(20)
      }, 0)
    })
  }
}
//-----------------------------------类似于
export default {
//子组件里面的定义
  data() {
    return {
      count: 0,
    }
  },
  methods: {
    addToCount(n) {
      this.count += n
      this.$emit('add-to-count', n)
    },
    resetCount() {
      this.count = 0
      this.$emit('reset')
    },
    returnValue() {
      this.$emit('return-value', 10)
    },
    onInputChange(e) {
      this.$emit('on-input-change', e.target.value, e)
    },
    promise() {
      const promise = new Promise((resolve) => {
        setTimeout(() => {
          resolve(20)
        }, 0)
      })
      promise.then((value) => {
        this.$emit('promise', value)
      })
    },
  },
}


3、@Model 装饰器
@Model 装饰器是用以组件上实现双向绑定
1)父组件

<template>
  <div class="home">
      <div>
        <h1>@Model - 组件上实现双向绑定</h1>
        <button type="primary" @click="onAlterFoo">父组件改变foo</button>
        <HelloWorld v-model="foo"></HelloWorld>
      </div>
  </div>
</template>
<script lang="ts">
import { Vue, Component, Model } from 'vue-property-decorator';
import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
@Component({ components: { HelloWorld } })
export default class ModelComponent extends Vue {
  private foo: boolean = true;
  private onAlterFoo() {
    this.foo = !this.foo;
  }
}
</script>

2)子组件

<template>
  <div>
    <h1>{{checked}}</h1>
  </div>
</template>
<script lang="ts">
import { Vue, Component, Model, Prop } from 'vue-property-decorator';    
@Component
export default class ModelComponent extends Vue {
  @Model('change', { type: Boolean }) private checked!: boolean;
}
</script>

//类似于
export default {
  model: {
    prop: 'checked',
    event: 'change',
  },
  props: {
    checked: {
      type: Boolean,
    },
  },
}

4、@Watch 装饰器
@Watch 装饰器是用以监控数据是否改变。

    <template>
      <div>
        <h1>@Watch用以监控数据是否改变</h1>
        <input v-model="dataWatch" placeholder="请输入内容"></input>
        <p>当前值:{{ newVal }} 原来值:{{ oldVal }}</p>
      </div>
    </template>    
    <script lang="ts">
    import { Component, Vue, Watch } from 'vue-property-decorator';    
    @Component
    export default class datasWatch extends Vue {
      private dataWatch: number | null = null;
      private newVal : number | null = null;
      private oldVal: number | null = null;    
      @Watch('dataWatch')
      private onChanged(newVal : number, oldVal: number): void {
        this.newVal = newVal ;
        this.oldVal = oldVal;
      }
    }
    </script>

5、@Provide 装饰器 和 @Inject 装饰器
@Provide 装饰器是用以注入数据
@Inject 装饰器是用以获取注入的数据
1)父组件 @Provide

<template>
  <div>
    <h1>@Provide/@Inject -接收来自父组件的数据</h1>
    父组件通过依赖注入赋予的值是:{{ foo }}
    <ProvideInject></ProvideInject>
  </div>
</template>
<script lang="ts">
import { Component, Vue, Provide } from 'vue-property-decorator';
import ProvideInject from '@/components/HelloWorld.vue';
@Component({ components: { ProvideInject } })
export default class Home extends Vue {
  @Provide('bar') private foo = '111';
}
</script>

2)子组件 @Inject

<template>
  <div>
    子组件通过依赖注入接收的值是:{{ bar }}
  </div>
</template>
<script lang="ts">
import { Component, Vue, Inject } from 'vue-property-decorator';
@Component
export default class ProvideInject extends Vue {
  @Inject() private readonly bar!: string;
}
</script>

6、@PropSync
@PropSync(propName: string, options: (PropOptions | Constructor[] | Constructor) = {}) decorator
import { Vue, Component, PropSync } from ‘vue-property-decorator’

@Component
export default class YourComponent extends Vue {
  @PropSync('name', { type: String }) syncedName!: string
}
//-------------------is equivalent to
export default {
  props: {
    name: {
      type: String,
    },
  },
  computed: {
    syncedName: {
      get() {
        return this.name
      },
      set(value) {
        this.$emit('update:name', value)
      },
    },
  },
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2.0是一款流行的JavaScript框架,用于构建用户界面。它具有良好的模块化和组件化特性,使开发人员能够更高效地开发可重用的前端代码。 而TypeScript(简称TS)则是一种由微软开发的开源编程语言,它是JavaScript的超集,为JavaScript添加了静态类型检查和更强大的开发工具支持。使用TS可以提高代码的可维护性和可读性。 后台模板是指在后端开发中,为了快速搭建项目的界面和功能,开发人员可以使用现有的模板来减少重复劳动。后台模板通常包含了常见的界面组件和功能,如登录页面、仪表盘、表格、图表等,开发人员只需要在此基础上进行定制化开发即可。 基于Vue2.0TS的后台模板提供了一种快速开发后台管理系统的解决方案。它结合了Vue2.0的响应式特性和组件化开发方式,以及TS的类型检查和开发工具支持,使开发人员能够高效地构建复杂的后台界面和功能。 使用Vue2.0TS开发后台模板具有以下好处: 1. 更高的代码可维护性和可读性,TS的静态类型检查可以帮助开发人员及时发现潜在的 bug,提早进行修复。 2. 开发效率更高,Vue2.0的组件化开发方式和TS的开发工具支持可以减少代码量和重复代码,提高开发效率。 3. 提供了丰富的UI组件和功能模块,如表格、图表、表单验证等,可以快速构建出功能完善的后台管理系统。 4. 可扩展性强,Vue2.0的模块化开发方式和TS的模块化功能可以方便地引入第三方库和插件,满足不同项目的需求。 总之,基于Vue2.0TS的后台模板是一种理想的解决方案,它结合了Vue2.0TS的优点,能够帮助开发人员快速构建高质量的后台管理系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值