一篇文章带你使用Typescript封装一个Vue组件(简单易懂)


Posted in Javascript onJune 05, 2020

一、搭建项目以及初始化配置

vue create ts_vue_btn

这里使用了vue CLI3自定义选择的服务,我选择了ts、stylus等工具。然后创建完项目之后,进入项目。使用快捷命令code .进入Vs code编辑器(如果没有code .,需要将编辑器的bin文件目录地址放到环境变量的path中)。然后,我进入编辑器之后,进入设置工作区,随便设置一个参数,这里比如推荐设置字号,点下。这里是为了生成.vscode文件夹,里面有个json文件。

一篇文章带你使用Typescript封装一个Vue组件(简单易懂)

我们在开发项目的时候,项目文件夹内的文件很多,会有时影响视觉。那么这个文件就是设置什么文件隐藏,注意只是隐藏,而不是删除!下面是我自己写的,在Vue cli3生成的项目需要隐藏的文件参数。

{
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/README.md": true,
    "**/node_modules":true,
    "**/shims-tsx.d.ts": true,
    "**/shims-vue.d.ts": true,
    "**/.browserslistrc": true,
    ".eslintrc.js": true,
    "babel.config.js": true,
    "package-lock.json": true,
    ".gitignore": true,
    "tsconfig.json": true
  }
}

以下就是所看到的文件目录,我把一些无关紧要的文件跟文件夹隐藏或者删除后所看到的。

一篇文章带你使用Typescript封装一个Vue组件(简单易懂)

文件解读(从上往下):

文件夹或文件 包含子文件夹或文件 含义
.vscode settings.json 隐藏文件设置
public index.html、favicon.ico 静态文件存放处
src components文件夹(存放组件)、App.vue、Home.vue、main.js 项目主要文件夹
package.json 项目依赖参数等

二、开发实践

下图为所需要创建的项目文件目录,这里我们开发一个Vue按钮组件。

一篇文章带你使用Typescript封装一个Vue组件(简单易懂)

如下图所示,这就是我们要用Typescript开发的组件。

一篇文章带你使用Typescript封装一个Vue组件(简单易懂)

开始编辑:

1、App.vue

<template>
 <div id="app">
  <Home></Home> 
 </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';// 编写类样式组件所需要的一些类或者是装饰器
import Home from "@/Home.vue"; // 引入页面组件

// 这里我们需要使用Component装饰器,这个装饰器是注册组件用的,里面的参数是一个对象,内有一个components属性,值为引入的组件名
@Component({
 components:{
  Home
 }
})
export default class App extends Vue {}
</script>

<style lang="stylus">

</style>

2、UIBtn.vue

<template>
 <!-- v-on="$listeners" 可以使用,在本类不再监听,在其他地方监听,可以不用$emit(),但是我们这里不用它 -->
 <button
  class="ui-btn"
  @click="onBtnclick('success!')"
  :class="{
  'ui-btn-xsmall':xsmall,
  'ui-btn-small':small,
  'ui-btn-large':large,
  'ui-btn-xlarge':xlarge
 }"
 >
  <slot>Button</slot>
 </button>
</template>

<script lang="ts">
import { Component, Vue, Emit, Prop } from "vue-property-decorator"; // 编写类样式组件所需要的一些类或者是装饰器
@Component
export default class UIBtn extends Vue {
 @Prop(Boolean) private xsmall: boolean | undefined;
 @Prop(Boolean) private small: boolean | undefined;
 @Prop(Boolean) private large: boolean | undefined;
 @Prop(Boolean) private xlarge: boolean | undefined;
 // eslint-disable-next-line @typescript-eslint/no-empty-function
 @Emit("click") private emitclick(x: string) {}
 private mounted() {
  console.log(this.large);
 }
 private onBtnclick(x: string) {
  this.emitclick(x);
 }
}
</script>

<style scoped lang="stylus" >
resize(a, b, c) 
 padding a b 
 font-size c
.ui-btn 
 resize(12px, 20px, 14px)
 border 0 solid #000
 border-radius 4px
 outline none
 font-weight 500;
 letter-spacing 0.09em
 background-color #409eff
 color #fff
 cursor pointer
 user-select none
 &:hover
  filter brightness(120%)
 &:active
  filter brightness(80%)
 &.ui-btn-xsmall 
  resize(5px, 15px, 14px)
 &.ui-btn-small 
  resize(8px, 18px, 14px)
 &.ui-btn-large 
  resize(14px, 22px, 14px)
 &.ui-btn-xlarge 
  resize(16px, 24px, 14px)
</style>

3、Home.vue

<template>
 <div class="home-con">
   <div class="btn-group">
      <UIBtn class="btn" @click="resize('xsmall')">超小</UIBtn>
      <UIBtn class="btn" @click="resize('small')">小</UIBtn>
      <UIBtn class="btn" @click="resize('normal')">正常</UIBtn>
      <UIBtn class="btn" @click="resize('large')">大</UIBtn>
      <UIBtn class="btn" @click="resize('xlarge')">超大</UIBtn>
   </div>
   <div class="btn-con">
      <UIBtn @click='onClick' 
      :xlarge="xlarge"
      :large="large"
      :small="small"
      :xsmall="xsmall"
      >主要按钮</UIBtn>
   </div>
   <div class="btn-pro">
      <UIBtn large >样式按钮</UIBtn>
   </div>  
 </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'; // 编写类样式组件所需要的一些类或者是装饰器
import UIBtn from '@/components/UIBtn.vue';
@Component({
  components:{
   UIBtn
  }
})
export default class Home extends Vue {
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
  private xlarge: boolean = false;
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
  private large: boolean = false;
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
  private xsmall: boolean = false;
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
  private small: boolean = false;
  private resize (name: string){
    console.log(name)
    switch (name) {
      case 'xsmall':
        this.xsmall=true;
        this.small=false;
        this.large=false;
        this.xlarge=false;
        break;
      case 'small':
        this.xsmall=false;
        this.small=true;
        this.large=false;
        this.xlarge=false;
        break;
      case 'normal':
        this.xsmall=false;
        this.small=false;
        this.large=false;
        this.xlarge=false;
        break;
      case 'large':
        this.xsmall=false;
        this.small=false;
        this.large=true;
        this.xlarge=false;
        break;
      case 'xlarge':
        this.xsmall=false;
        this.small=false;
        this.large=false;
        this.xlarge=true;
        break;
    }
  }
  private onClick(x: string) {
    console.log(x)
  }
}
</script>

<style lang="stylus" scoped>
.btn-group
  margin 50px 0
.btn
  margin 6px
.btn-pro
  margin-top 50px 
</style>

到此这篇关于一篇文章带你使用Typescript封装一个Vue组件的文章就介绍到这了,更多相关Typescript封装Vue组件内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
最简单的js图片切换效果实现代码
Sep 24 Javascript
jQuery 隐藏和显示 input 默认值示例
Jun 03 Javascript
在 Express 中使用模板引擎
Dec 10 Javascript
Angular.js之作用域scope'@','=','&amp;'实例详解
Feb 28 Javascript
vue.js事件处理器是什么
Mar 20 Javascript
微信小程序实战之仿android fragment可滑动底部导航栏(4)
Apr 16 Javascript
JS设计模式之策略模式概念与用法分析
Feb 05 Javascript
vue之浏览器存储方法封装实例
Mar 15 Javascript
详解开发react应用最好用的脚手架 create-react-app
Apr 24 Javascript
Angular2中监听数据更新的方法
Aug 31 Javascript
在微信小程序中使用vant的方法
Jun 07 Javascript
详解小程序如何改变onLoad的执行时机
Nov 01 Javascript
vscode 插件开发 + vue的操作方法
Jun 05 #Javascript
vue渲染方式render和template的区别
Jun 05 #Javascript
Vue是怎么渲染template内的标签内容的
Jun 05 #Javascript
Vue 如何使用props、emit实现自定义双向绑定的实现
Jun 05 #Javascript
VueX模块的具体使用(小白教程)
Jun 05 #Javascript
Vuex的热更替如何实现
Jun 05 #Javascript
2分钟实现一个Vue实时直播系统的示例代码
Jun 05 #Javascript
You might like
PHP strncasecmp字符串比较的小技巧
2011/01/04 PHP
利用ThinkPHP内置的ThinkAjax实现异步传输技术的实现方法
2011/12/19 PHP
php中选择什么接口(mysql、mysqli)访问mysql
2013/02/06 PHP
Yii框架引用插件和ckeditor中body与P标签去除的方法
2017/01/19 PHP
php使用curl伪造来源ip和refer的方法示例
2018/05/08 PHP
PHP使用HTML5 FileApi实现Ajax上传文件功能示例
2019/07/01 PHP
javascript AOP 实现ajax回调函数使用比较方便
2010/11/20 Javascript
eval与window.eval的差别分析
2011/03/17 Javascript
jquery创建一个ajax关键词数据搜索实现思路
2013/02/26 Javascript
解析JavaScript中的不可见数据类型
2013/12/02 Javascript
js自定义回调函数
2015/12/13 Javascript
基于javascript实现句子翻牌网页版小游戏
2016/03/23 Javascript
使用jQuery.form.js/springmvc框架实现文件上传功能
2016/05/12 Javascript
JavaScript中const、var和let区别浅析
2016/10/11 Javascript
jQuery图片轮播功能实例代码
2017/01/29 Javascript
vue中用H5实现文件上传的方法实例代码
2017/05/27 Javascript
jQuery实现的卷帘门滑入滑出效果【案例】
2019/02/18 jQuery
如何在vue中使用kindeditor富文本编辑器
2020/12/19 Vue.js
[01:20:30]OG vs LGD 2018国际邀请赛淘汰赛BO3 第四场 8.26
2018/08/30 DOTA
利用Python脚本在Nginx和uwsgi上部署MoinMoin的教程
2015/05/05 Python
浅谈Python生成器generator之next和send的运行流程(详解)
2017/05/08 Python
Python中fnmatch模块的使用详情
2018/11/30 Python
用python爬取租房网站信息的代码
2018/12/14 Python
解决Python3.8用pip安装turtle-0.0.2出现错误问题
2020/02/11 Python
python实现录音功能(可随时停止录音)
2020/10/26 Python
python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法
2020/11/05 Python
python基于openpyxl生成excel文件
2020/12/23 Python
详解HTML5中的Communication API基本使用方法
2016/01/29 HTML / CSS
玛蒂尔达简服装:Matilda Jane Clothing
2019/02/13 全球购物
自动化工程专业个人应聘自荐信
2013/09/26 职场文书
护士思想汇报
2014/01/12 职场文书
食堂采购员岗位职责
2015/04/03 职场文书
忠犬八公的故事观后感
2015/06/05 职场文书
军训心得体会范文(2016最新篇)
2016/01/11 职场文书
手把手教你使用TensorFlow2实现RNN
2021/07/15 Python
python运行脚本文件的三种方法实例
2022/06/25 Python