一篇文章带你使用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 相关文章推荐
firefo xml 读写实现js代码
Jun 11 Javascript
JSON 和 JavaScript eval使用说明
Jun 13 Javascript
js 静态动态成员 and 信息的封装和隐藏
May 29 Javascript
JQuery获取当前屏幕的高度宽度的实现代码
Jul 12 Javascript
javascript强制点击广告的方法
Feb 06 Javascript
javascript实现状态栏中文字动态显示的方法
Oct 20 Javascript
微信小程序 教程之列表渲染
Oct 18 Javascript
Angularjs分页查询的实现
Feb 24 Javascript
JS实现二叉查找树的建立以及一些遍历方法实现
Apr 17 Javascript
微信小程序页面传值实例分析
Apr 19 Javascript
element form 校验数组每一项实例代码
Oct 10 Javascript
vue仿淘宝滑动验证码功能(样式模仿)
Dec 10 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动态生成静态HTML网页的代码
2010/03/04 PHP
php操作mongoDB实例分析
2014/12/29 PHP
php调用自己java程序的方法详解
2016/05/13 PHP
Yii2选项卡的简单使用
2017/05/26 PHP
js模仿windows桌面图标排列算法具体实现(附图)
2013/06/16 Javascript
jquery win 7透明弹出层效果的简单代码
2013/08/06 Javascript
基于MVC3方式实现下拉列表联动(JQuery)
2013/09/02 Javascript
JQuery+Ajax无刷新分页的实例代码
2014/02/08 Javascript
JS实现网页上随机产生超链接地址的方法
2015/11/09 Javascript
canvas 实现中国象棋
2017/02/17 Javascript
vue.js组件vue-waterfall-easy实现瀑布流效果
2017/08/22 Javascript
微信小程序wepy框架学习和使用心得详解
2019/05/24 Javascript
vue.config.js中配置Vue的路径别名的方法
2020/02/11 Javascript
ES6扩展运算符和rest运算符用法实例分析
2020/05/23 Javascript
vue实现简易的双向数据绑定
2020/12/29 Vue.js
[02:51]DOTA2英雄基础教程 风暴之灵
2013/12/23 DOTA
[48:48]完美世界DOTA2联赛PWL S3 Magama vs GXR 第一场 12.19
2020/12/24 DOTA
python list使用示例 list中找连续的数字
2014/01/27 Python
Python itertools模块详解
2015/05/09 Python
python 移除字符串尾部的数字方法
2018/07/17 Python
Python将文字转成语音并读出来的实例详解
2019/07/15 Python
python实现猜单词游戏
2020/05/22 Python
CSS中越界问题的经典解决方案【推荐】
2016/04/19 HTML / CSS
使用HTML5 Canvas API控制字体的显示与渲染的方法
2016/03/24 HTML / CSS
Lookfantastic澳大利亚官网:英国知名美妆购物网站
2021/01/07 全球购物
车工岗位职责
2013/11/26 职场文书
中国梦的演讲稿
2014/01/08 职场文书
化学系大学生自荐信范文
2014/03/01 职场文书
员工工作能力评语
2014/12/31 职场文书
监守自盗观后感
2015/06/10 职场文书
绿里奇迹观后感
2015/06/15 职场文书
2016年父亲节寄语
2015/12/04 职场文书
2016关于预防职务犯罪的心得体会
2016/01/21 职场文书
教师个人教学反思
2016/02/23 职场文书
python tqdm用法及实例详解
2021/06/16 Python
一文了解MySQL二级索引的查询过程
2022/02/24 MySQL