欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章帶你使用Typescript封裝一個Vue組件(簡單易懂)

 更新時間:2020年06月05日 10:54:58   作者:Vam的金豆之路  
這篇文章主要介紹了使用Typescript封裝一個Vue組件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

vue create ts_vue_btn

這里使用了vue CLI3自定義選擇的服務(wù),我選擇了ts、stylus等工具。然后創(chuàng)建完項目之后,進(jìn)入項目。使用快捷命令code .進(jìn)入Vs code編輯器(如果沒有code .,需要將編輯器的bin文件目錄地址放到環(huán)境變量的path中)。然后,我進(jìn)入編輯器之后,進(jìn)入設(shè)置工作區(qū),隨便設(shè)置一個參數(shù),這里比如推薦設(shè)置字號,點下。這里是為了生成.vscode文件夾,里面有個json文件。

我們在開發(fā)項目的時候,項目文件夾內(nèi)的文件很多,會有時影響視覺。那么這個文件就是設(shè)置什么文件隱藏,注意只是隱藏,而不是刪除!下面是我自己寫的,在Vue cli3生成的項目需要隱藏的文件參數(shù)。

{
  "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
  }
}

以下就是所看到的文件目錄,我把一些無關(guān)緊要的文件跟文件夾隱藏或者刪除后所看到的。


文件解讀(從上往下):

文件夾或文件 包含子文件夾或文件 含義
.vscode settings.json 隱藏文件設(shè)置
public index.html、favicon.ico 靜態(tài)文件存放處
src components文件夾(存放組件)、App.vue、Home.vue、main.js 項目主要文件夾
package.json 項目依賴參數(shù)等

二、開發(fā)實踐

下圖為所需要創(chuàng)建的項目文件目錄,這里我們開發(fā)一個Vue按鈕組件。


如下圖所示,這就是我們要用Typescript開發(fā)的組件。

開始編輯:

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裝飾器,這個裝飾器是注冊組件用的,里面的參數(shù)是一個對象,內(nèi)有一個components屬性,值為引入的組件名
@Component({
 components:{
  Home
 }
})
export default class App extends Vue {}
</script>

<style lang="stylus">

</style>

2、UIBtn.vue

<template>
 <!-- v-on="$listeners" 可以使用,在本類不再監(jiān)聽,在其他地方監(jiān)聽,可以不用$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>

到此這篇關(guān)于一篇文章帶你使用Typescript封裝一個Vue組件的文章就介紹到這了,更多相關(guān)Typescript封裝Vue組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue中v-for更新檢測的操作方法

    Vue中v-for更新檢測的操作方法

    v-for 指令需要使用 item in items 形式的特殊語法,其中 items 是源數(shù)據(jù)數(shù)組,而 item 則是被迭代的數(shù)組元素的別名。今天通過本文給大家介紹Vue中v-for更新檢測的操作方法,感興趣的朋友一起看看吧
    2021-10-10
  • vue3中7種路由守衛(wèi)的使用大全舉例

    vue3中7種路由守衛(wèi)的使用大全舉例

    最近在學(xué)習(xí)vue,感覺路由守衛(wèi)這個地方知識點挺多的,而且很重要,下面這篇文章主要給大家介紹了關(guān)于vue3中7種路由守衛(wèi)的使用大全,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • Vue3+TS項目中eslint、prettier安裝配置詳細(xì)指南

    Vue3+TS項目中eslint、prettier安裝配置詳細(xì)指南

    為了更好的統(tǒng)一項目的代碼風(fēng)格,因此在編寫時就可以使用eslint+prettier,它們不僅能方便代碼編寫,還能避免不必要的錯誤,讓代碼變得更加嚴(yán)謹(jǐn),這篇文章主要給大家介紹了關(guān)于Vue3+TS項目中eslint、prettier安裝配置的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • 解決Electron?store的commit和dispatch不好用問題

    解決Electron?store的commit和dispatch不好用問題

    這篇文章主要介紹了解決Electron?store的commit和dispatch不好用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • element表格el-table實現(xiàn)虛擬滾動解決卡頓問題

    element表格el-table實現(xiàn)虛擬滾動解決卡頓問題

    當(dāng)頁面數(shù)據(jù)過多,前端渲染大量的DOM時,會造成頁面卡死問題,本文主要介紹了element表格el-table實現(xiàn)虛擬滾動解決卡頓問題,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • Vue表單數(shù)據(jù)修改與刪除功能實現(xiàn)

    Vue表單數(shù)據(jù)修改與刪除功能實現(xiàn)

    本文通過實例代碼介紹了Vue表單數(shù)據(jù)修改與刪除功能實現(xiàn),結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友跟隨小編一起看看吧
    2023-10-10
  • 詳細(xì)聊聊vue中組件的props屬性

    詳細(xì)聊聊vue中組件的props屬性

    父子組件之間的通信就是props down,events up,父組件通過屬性props向下傳遞數(shù)據(jù)給子組件,子組件通過事件events 給父組件發(fā)送消息,這篇文章主要給大家介紹了關(guān)于vue中組件的props屬性的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 解決vue 路由變化頁面數(shù)據(jù)不刷新的問題

    解決vue 路由變化頁面數(shù)據(jù)不刷新的問題

    下面小編就為大家分享一篇解決vue 路由變化頁面數(shù)據(jù)不刷新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue編寫的功能強(qiáng)大的swagger-ui頁面及使用方式

    vue編寫的功能強(qiáng)大的swagger-ui頁面及使用方式

    swagger是一種標(biāo)準(zhǔn)的數(shù)據(jù)格式的定義,對于不同語言進(jìn)行實現(xiàn)一些注解API式的東西,能快速生成這種描述restful格式的api信息的json串,本文給大家詳細(xì)介紹vue編寫的功能強(qiáng)大的swagger-ui頁面,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • vue.extend實現(xiàn)alert模態(tài)框彈窗組件

    vue.extend實現(xiàn)alert模態(tài)框彈窗組件

    這篇文章主要為大家詳細(xì)介紹了vue.extend實現(xiàn)alert模態(tài)框彈窗組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評論