uni-app 創建typescript 項目 並且引入 vant


前言

在小程序上應用uniapp 然后引入ts ,你說香不香,哈哈哈 

好了 先來一發教程:

官網下載 軟件 https://uniapp.dcloud.io/

我創建的TS項目是,用cli 命令行創建的

全局安裝vue-cli

npm install -g @vue/cli

創建uni-app

使用正式版(對應HBuilderX最新正式版)

vue create -p dcloudio/uni-preset-vue my-ts

 

 

選中 默認模板 (typeScript ) 然后 回車等待

然后在用編輯器打開 

 

你會發現 目錄是這樣的:

 

 

 小程序 page 里面就可以頁面啦

在新項目的vue文件中使用內聯ts

<script lang="ts">

按需引入vue裝飾器

import { Component,Vue ,Watch} from "vue-property-decorator";

不管干啥先把下面這句話加上。

@Component({}) //必須

常見裝飾器的使用

export default class Idnex extends Vue{
private title:String = 'myTitle'; //響應式屬性
private num:Number = 123; //對標之前的data函數返回的對象
get age():Number{ //計算屬性
return this.num;
}
onLoad(){
this.printTitle();
let a:string = '123'; 
}
@Watch('title') //watch,此處是監聽title的變化
titleChange(newVal:Number,oldVal:Number){
console.log(newVal,oldVal);
}
printTitle():void{ //methods
console.log('hahahhhaha')
}

}

簡單的Demo頁面

<template>
    <view class="content" @click.self="printTitle">
        <image class="logo" src="/static/logo.png" @click.stop="title = 'ggg'"></image>
        <view class="text-area">
            <text class="title">{{title}}</text>
            <view>{{age}}</view>
        </view>
    </view>
</template>

<script lang="ts">
    import { Component,Vue ,Watch} from "vue-property-decorator";
    @Component({})
    export default class Idnex extends Vue{
        private title:String  = 'myTitle'; //響應式屬性
        private num:Number = 123;           //對標之前的data函數返回的對象
        get age():Number{                   //計算屬性
            return this.num;
        }
        onLoad(){
            this.printTitle();
            let a:string = '123'; 
        }
        @Watch('title')                    //watch,此處是監聽title的變化
        titleChange(newVal:Number,oldVal:Number){
            console.log(newVal,oldVal);
        }
        printTitle():void{                //methods
            console.log('hahahhhaha')
        }
        
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

其實我的index 頁面是這樣 的

<template>
<view class="content">
  <image class="logo" :src="'../../static/logo.png'"></image>
  <view>
    <view v-for="(title, key) in titles" :key="key" class="title">{{title}}</view>
  </view>
</view>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'

@Component
export default class Home extends Vue {
  titles: string[] = ['title1', 'title2'];
  onLoad() {
    console.log(this.titles)
  }
}
</script>

<style>
.content {
  text-align: center;
  height: 400upx;
}
.logo{
    height: 200upx;
    width: 200upx;
    margin-top: 200upx;
}
.title {
  font-size: 36upx;
  color: #8f8f94;
}
</style>
View Code

然后 運行 開發 小程序 你在小程序上就可以看到了

 

 

 

 

是不是很 開心 哈哈

 

引入vant

 

  • 項目根目錄下新建 wxcomponents 目錄 ,此目錄應該與components 目錄同級。

  • 直接通過 git 下載 vant-weapp 最新源代碼,並將dist目錄拷貝到新建的wxcomponents目錄下,並重命名dist@vant/weapp。

在你的 pages.json 文件下寫

 "globalStyle": {
    "backgroundColor": "light",
    "navigationBarBackgroundColor": "#fd4d67",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "white",
    "usingComponents": {
      "van-button": "wxcomponents/@vant/weapp/button/index",
      "van-datetime-picker": "wxcomponents/@vant/weapp/datetime-picker/index",
      "van-area": "wxcomponents/@vant/weapp/area/index",
      "van-checkbox": "wxcomponents/@vant/weapp/checkbox/index",
      "van-checkbox-group": "wxcomponents/@vant/weapp/checkbox-group/index",
      "van-picker": "wxcomponents/@vant/weapp/picker/index",
      "van-cell": "wxcomponents/@vant/weapp/cell/index",
      "van-cell-group": "wxcomponents/@vant/weapp/cell-group/index",
      "van-popup": "wxcomponents/@vant/weapp/popup/index",
      "van-tabs": "wxcomponents/@vant/weapp/tabs/index",
      "van-tab": "wxcomponents/@vant/weapp/tab/index",
      "van-radio": "wxcomponents/@vant/weapp/radio/index",
      "van-radio-group": "wxcomponents/@vant/weapp/radio-group/index",
      "van-tag": "wxcomponents/@vant/weapp/tag/index",
      "van-field": "wxcomponents/@vant/weapp/field/index",
      "van-toast": "wxcomponents/@vant/weapp/toast/index",
      "van-switch": "wxcomponents/@vant/weapp/switch/index",
      "van-divider": "wxcomponents/@vant/weapp/divider/index",
      "van-action-sheet": "wxcomponents/@vant/weapp/action-sheet/index",
      "van-dialog": "wxcomponents/@vant/weapp/dialog/index",
      "van-swipe-cell": "wxcomponents/@vant/weapp/swipe-cell/index",
      "van-index-bar": "wxcomponents/@vant/weapp/index-bar/index",
      "van-index-anchor": "wxcomponents/@vant/weapp/index-anchor/index",
      "van-loading": "wxcomponents/@vant/weapp/loading/index"
    }

 

3. 頁面中使用引入的UI組件

在App.Vue文件中style部分引入UI組件庫

<script lang="ts">
    export default {
        onLaunch: function() {
            console.log('App Launch')
        },
        onShow: function() {
            console.log('App Show')
        },
        onHide: function() {
            console.log('App Hide')
        }
    }
</script>

<style>
    @import "/wxcomponents/@vant/weapp/common/index.wxss";
    /*每個頁面公共css */
</style>

然后就可以愉快的開始 用ts 和 vant啦

BOOM shaka laka la 哈哈, 開始你的uniapp TS  之旅吧 

面對疾風吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM