前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue-class-component

vue-class-component

作者头像
阿超
发布2022-08-21 14:40:46
4620
发布2022-08-21 14:40:46
举报
文章被收录于专栏:快乐阿超

官方文档:https://class-component.vuejs.org/

class-componentvue官方库之一,其可以让你使用class的方式定义、编写组件

再加上ts的装饰器,最终效果如下:

代码语言:javascript
复制
<template>
  <div>
    <button v-on:click="decrement">-</button>
    {{ count }}
    <button v-on:click="increment">+</button>
  </div>
</template>

<script>
import Vue from 'vue'
import Component from 'vue-class-component'

// Define the component in class-style
@Component
export default class Counter extends Vue {
  // Class properties will be component data
  count = 0

  // Methods will be component methods
  increment() {
    this.count++
  }

  decrement() {
    this.count--
  }
}
</script>

同样,其可以配合tsx使用,这样就越来越像我们的react

代码语言:javascript
复制
<script lang="tsx">
import { Component, Prop, Vue } from 'vue-property-decorator';

@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
  count = 0;

  increment() {
    this.count++;
  }

  get twiceTheCounter() {
    return this.count * 2;
  }

  mounted() {
    console.log('mounted')
  }

  // Declare render function
  render() {
    return (
      <div>
        <div>{this.msg}</div>
        <p>You clicked {this.count} times</p>
        <p>{this.twiceTheCounter}</p>
        <button onClick={this.increment}>Click me</button>
      </div>)
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档