vue - - - - - vue-property-decorator的使用

哪有小孩天天哭,哪有赌徒天天输 。遇到不会的技术、知识点,看得多了,掉的坑多了,也就会了。

1. 单文件组件写法 - @Component的使用

<template>
	<div class="study" id="root">
    	I like learning
    	<hr/>
	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';
    @Component({})  
    export default class index extends Vue { 
	}
</script>
<style lang="scss">
</style>

效果如图:
在这里插入图片描述

2. 组件内使用变量

<template>
	<div class="study" id="root">
	    I like learning
	    <hr/>
	    
	    <!-- 组件内变量使用 -->
	    姓名: {{name}}<br/>
	    性别: {{gender}}<br/>
	    年龄: {{age}}<br/>
	    <hr/>
	    
	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';
    @Component({})  
    export default class index extends Vue { 
      name:string = 'lee'
      gender:string = '男'
      age:number = 18
	}
</script>
<style lang="scss">
</style>

效果如图:
在这里插入图片描述

3. 使用计算属性 - get的使用

<template>
	<div class="study" id="root">
	    I like learning
	    <hr/>
	    
	    <!-- 组件内变量使用 -->
	    姓名: {{name}}<br/>
	    性别: {{gender}}<br/>
	    年龄: {{age}}<br/>
	    <hr/>
	    
	    <!-- 组件内使用getter计算属性 -->
    	使用计算属性:{{concatenatedCharacter}}
    	<hr/>
	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';
    @Component({})  
    export default class index extends Vue { 
       name:string = 'lee'
       gender:string = '男'
       age:number = 18


	    // 定义计算属性
        get concatenatedCharacter(){
            return `我叫${this.name}${this.gender},今年${this.age}`
        }
   }
</script>
<style lang="scss">
</style>

效果如图:
在这里插入图片描述

4. 生命周期

<template>
	<div class="study" id="root">
    I like learning
    <hr>

    <!-- 组件内变量使用 -->
    姓名: {{name}}<br/>
    性别: {{gender}}<br/>
    年龄: {{age}}<br/>
    <hr>


    <!-- 组件内使用getter计算属性 -->
    <!-- 使用计算属性:{{concatenatedCharacter}} -->
    <!-- <hr> -->
	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';  
  @Component({})  
  export default class index extends Vue { 
    // 组件内定义变量
    name:string = 'lee'
    gender:string = '男'
    age:number = 18


    // 定义计算属性
    get concatenatedCharacter(){
      return `我叫${this.name}${this.gender},今年${this.age}`
    }

	// 生命周期
    created() {
      console.log('我是生命周期');
    }
    
  }
</script>
<style lang="scss">
</style>

效果如图:
在这里插入图片描述

5. methods方法

<template>
	<div class="study" id="root">
	    I like learning
	    <hr>
	
	    <!-- 组件内变量使用 -->
	    姓名: {{name}}<br/>
	    性别: {{gender}}<br/>
	    年龄: {{age}}<br/>
	    <hr>
	
	    <!-- 年龄+1 -->
	    <button @click="increaseInAge"> 年龄+1 </button>
	    <hr>

	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';  
  @Component({})  
  export default class index extends Vue { 
    // 组件内定义变量
    name:string = 'lee'
    gender:string = '男'
    age:number = 18


    // 定义计算属性
    get concatenatedCharacter(){
      return `我叫${this.name}${this.gender},今年${this.age}`
    }

    created() {
      console.log('我是生命周期');
    }

    // methods方法定义: 年龄+1
    increaseInAge(){
      this.age++
    }
    
  }
</script>
<style lang="scss">
</style>

效果图:
在这里插入图片描述

6. 使用自定义组件

6.1 父传子 - @Prop的使用

父组件:

<template>
	<div class="study" id="root">
	    I like learning
	    <hr>
	
	    <!-- 组件内变量使用 -->
	    姓名: {{name}}<br/>
	    性别: {{gender}}<br/>
	    年龄: {{age}}<br/>
	    <hr>
	
	    <!-- 年龄+1 -->
	    <button @click="increaseInAge"> 年龄+1 </button>
	    <hr>
	
	    <!-- 使用自定义组件 & 传递参数 -->
	    <CustomComp :age='age'/>
	    <hr>

	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';  
  import CustomComp from './CustomComp.vue'
  @Component({
    // 声明组件
    components: {
      CustomComp
    }
  })  
  export default class index extends Vue { 
    // 组件内定义变量
    name:string = 'lee'
    gender:string = '男'
    age:number = 18


    // 定义计算属性
    get concatenatedCharacter(){
      return `我叫${this.name}${this.gender},今年${this.age}`
    }

    created() {
      console.log('我是生命周期');
    }

    // 年龄+1
    increaseInAge(){
      this.age++
    }
    
  }
</script>
<style lang="scss">
</style>

子组件:

<template>
	<div class="study" id="root">
      I am a subcomponent; <br/>
      接收到的数据: {{age}}
	</div>
</template>
<script lang="ts">  
  import { Vue, Component, Prop } from 'vue-property-decorator';  
  @Component({})  
  export default class CustomComp extends Vue {  
    @Prop() age!: Number

  };  
</script>
<style lang='scss' scoped>
</style>

效果如图:
在这里插入图片描述

6.2 子传父 - @Emit的使用

子组件:

<template>
	<div class="study" id="root">
      I am a subcomponent; <br/>
      接收到的数据: {{age}}<br/>
      <button @click="childMethod">子传父</button>

	</div>
</template>
<script lang="ts">  
  import { Vue, Component, Prop, Emit } from 'vue-property-decorator';  
  @Component({})  
  export default class CustomComp extends Vue {  
    @Prop() age!: Number

    // emitToFather为父组件自定义的方法名
    // childMethod为子组件的方法名
    @Emit("emitToFather")
    childMethod() {
      console.log('我是子组件的方法');
      return 123
    }
  };  
</script>
<style lang='scss' scoped>
</style>

父组件:

<template>
	<div class="study" id="root">
	    I like learning
	    <hr>
	
	    <!-- 组件内变量使用 -->
	    姓名: {{name}}<br/>
	    性别: {{gender}}<br/>
	    年龄: {{age}}<br/>
	    <hr>
	
	    <!-- 年龄+1 -->
	    <button @click="increaseInAge"> 年龄+1 </button>
	    <hr>
	
	    <!-- 使用自定义组件 & 传递参数 -->
	    <CustomComp :age='age' @emitToFather='updateAge'/>
	    <hr>

	</div>
</template>

<script lang="ts">  
  import { Vue, Component } from 'vue-property-decorator';  
  import CustomComp from './CustomComp.vue'
  @Component({
    // 声明组件
    components: {
      CustomComp
    }
  })  
  export default class index extends Vue { 
    // 组件内定义变量
    name:string = 'lee'
    gender:string = '男'
    age:number = 18


    // 定义计算属性
    get concatenatedCharacter(){
      return `我叫${this.name}${this.gender},今年${this.age}`
    }

    created() {
      console.log('我是生命周期');
    }

    // 年龄+1
    increaseInAge(){
      this.age++
      console.log(`年龄增加1,现在是${this.age}`);
    }

    // 子传父 修改数据  
    updateAge(age: number) {
      console.log('我是父组件的方法, 接收到的数据为:', age);
      this.age = age;
    }
    
  }
</script>
<style lang="scss">
</style>

效果如图:
在这里插入图片描述

7. 监听变量变化 - @Watch的使用

<template>
	<div class="study" id="root">
      I am a subcomponent; <br/>
      接收到的数据: {{age}}<br/>
      <button @click="childMethod">子传父</button>

	</div>
</template>
<script lang="ts">  
  import { Vue, Component, Prop, Emit, Watch} from 'vue-property-decorator';  
  @Component({})  
  export default class CustomComp extends Vue {  
    @Prop() age!: Number

    // emitToFather为父组件自定义的方法名
    // childMethod为子组件的方法名
    @Emit("emitToFather")
    childMethod() {
      console.log('我是子组件的方法');
      return 123
    }

    // 监听age的变化
    // 当age发生变化时,会执行watchCallBack函数,并将旧值与新值一并传入
    @Watch("age", {deep: true})
    watchCallBack(val: number, oldVal: number) {
      console.log('val,oldVal: ', val, oldVal);
    }

  };  
</script>
<style lang='scss' scoped>
</style>

效果如图:
在这里插入图片描述

8. 双向绑定 - @Model的使用

此处再次演示了计算属性的用法

<template>
	<div class="study" id="root">
      I am a subcomponent; <br/>
      接收到的数据: {{age}}<br/>
      <button @click="childMethod">子传父</button><br/>
      
      双向绑定Model: <br/>
      <input type="text" v-model="people.firstName"><br/>
      <input type="text" v-model="people.lastName"><br/>
      姓名:{{ fullName }}<br/>
      <button @click="logModel"> log model</button><br/>

	</div>
</template>
<script lang="ts">  
  import { Vue, Component, Prop, Emit, Watch} from 'vue-property-decorator';  
  @Component({})  
  export default class CustomComp extends Vue {  
    @Prop() age!: Number

    people: any = {
      firstName: '',
      lastName: ''
    }

    // emitToFather为父组件自定义的方法名
    // childMethod为子组件的方法名
    @Emit("emitToFather")
    childMethod() {
      console.log('我是子组件的方法');
      return 123
    }

    // 监听age的变化
    // 当age发生变化时,会执行watchCallBack函数,并将旧值与新值一并传入
    @Watch("age", {deep: true})
    watchCallBack(val: number, oldVal: number) {
      console.log('val,oldVal: ', val, oldVal);
    }

    get fullName(){
      const { firstName, lastName } = this.people
      return `${firstName}-${lastName}`
    }
    logModel(){
      console.log('model数据', this.people);
    }

  };  
</script>
<style lang='scss' scoped>
</style>

效果如图:

在这里插入图片描述



更多可参考下列文档:
vue-property-decorator用法
聊聊在Vue项目中使用Decorator装饰器

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: vue-property-decorator 是一个用于简化 Vue.js 组件开发的装饰器库,而 Vue 3 是 Vue.js 的下一个主要版本。vue-property-decorator 目前仅适用于 Vue 2.x 版本,并在 Vue 3.x 中不再兼容。 Vue 3 意味着对 Vue.js 进行了大规模重写和重构,旨在提供更高性能、更小的包大小、更好的开发体验和更多功能。Vue 3 引入了许多新特性,例如 Composition API、Teleport、Suspense 等。 由于 Vue 3 中改编了大量的底层代码,vue-property-decoratorVue 3.x 中不再适用,这是因为 Vue 3 在其组件定义和交互方式上引入了一些改变。为了使现有的代码能够在 Vue 3.x 中运行,我们需要采用其他方式来替代 vue-property-decorator,用于装饰 Vue 组件的功能。 Vue 3 中提供了一种新的组合式 API - Composition API,它允许我们使用函数的形式组织和复用逻辑。通过 Composition API,我们可以在 Vue 组件中按照逻辑功能划分代码,使得组件更加清晰和可维护。虽然和 vue-property-decorator 的装饰器方式不同,但 Composition API 提供了一种更灵活和直观的方式来开发 Vue 组件。 总结来说,vue-property-decorator 是用于 Vue 2.x 版本的装饰器库,而 Vue 3.x 引入了新的 Composition API,取代了 vue-property-decorator,提供了更好的开发体验和更多的功能。所以在 Vue 3.x 中使用 vue-property-decorator 是不可行的,我们需要使用 Composition API 来替代。 ### 回答2: vue-property-decorator是一个用于在Vue组件中使用装饰器语法的库。它使我们能够更加简洁和优雅地编写Vue组件,同时提供了一些强大的功能和特性。 首先,vue-property-decorator允许我们使用装饰器来定义组件的属性,方法和计算属性。通过使用装饰器,我们可以将组件的相关代码集中在一起,使代码更加易读和易维护。例如,我们可以使用@Prop装饰器来定义组件的属性,并指定其类型和默认值。我们还可以使用@Watch装饰器来监听属性或计算属性的变化。 其次,vue-property-decorator提供了一些方便的装饰器来简化对组件生命周期钩子的使用。我们可以使用@Emit装饰器来触发自定义事件,使用@Provide装饰器来提供数据,使用@Inject装饰器来注入数据等。这些装饰器使我们能够更加方便地在组件之间进行通信和共享数据。 此外,vue-property-decorator还提供了一些装饰器来简化对Vue指令和插件的使用。通过使用@Directive装饰器,我们可以定义自定义指令并将其应用到组件中,而无需手动注册和使用指令。类似地,使用@Plugin装饰器,我们可以将第三方插件集成到组件中,从而增强组件的功能。 总而言之,vue-property-decorator是一个强大而灵活的库,它使我们能够更加方便地使用装饰器语法编写Vue组件。它提供了一些强大的功能和特性,使我们的代码更加简洁和易读。对于使用Vue 3的开发者来说,vue-property-decorator无疑是一个很好的选择。 ### 回答3: vue-property-decorator是一个用于Vue.js框架的装饰器库,它能够简化开发者在使用类风格的语法时对Vue组件属性的管理和操作。 在Vue.js 3版本中,vue-property-decorator已经进行了更新和迁移,与Vue.js 2版本的使用稍有区别。在Vue.js 3中,可以使用新的@Options装饰器以替代之前的@Component装饰器,同时也可以继续使用@property装饰器来声明和管理组件的属性。 @Options装饰器可以用于修饰一个Vue组件类,它接受一个配置对象作为参数,用来定义组件的选项和行为。例如: ``` import { Options, Vue } from 'vue-property-decorator'; @Options({ props: { message: { type: String, required: true } } }) export default class MyComponent extends Vue { // 组件的其他逻辑... } ``` 上述代码中,使用@Options装饰器修饰了一个Vue组件类,并通过props字段定义了一个名为message的属性,类型为String,且是必需的。 同时,我们仍然可以使用@property装饰器来声明和管理组件的其他属性。例如: ``` import { Options, Vue, property } from 'vue-property-decorator'; @Options({}) export default class MyComponent extends Vue { @property({ type: String }) message: string = ''; // 组件的其他逻辑... } ``` 上述代码中,使用@property装饰器修饰了一个名为message的属性,并指定了它的类型为String。 总的来说,vue-property-decorator能够简化Vue.js 3中使用类风格语法时对组件属性的管理和操作,提供了方便的装饰器来声明和定义组件的属性,并与Vue的选项和行为进行集成。使用它可以使代码更易读、更易维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值