這篇文章將為大家詳細(xì)講解有關(guān)vue-cli3+typescript的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供達(dá)坂城網(wǎng)站建設(shè)、達(dá)坂城做網(wǎng)站、達(dá)坂城網(wǎng)站設(shè)計、達(dá)坂城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、達(dá)坂城企業(yè)網(wǎng)站模板建站服務(wù),十年達(dá)坂城做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。vue-cli3
vue-cli3的詳細(xì)功能推薦官方文檔,不在本文介紹范圍內(nèi)。
安裝:
npm install -g @vue/cli
檢查安裝成功與否:
vue --version
創(chuàng)建項目:
vue create myapp
可以選擇Manually select feature
來自由選擇功能,常用的有vuex、vue-router、CSS Pre-processors等,我們再把typescript勾上,就可以回車進(jìn)入下一步了。PS:勾選的操作是按空格鍵。
創(chuàng)建成功之后,執(zhí)行啟動命令:
npm run serve
就可以通過http://localhost:8080/
訪問本地項目啦。
typescript
如果沒有typescript基礎(chǔ),可以先補(bǔ)補(bǔ)課,大概花三十分鐘就可以了解typescript的一些特性,比如:TypeScript 入門教程。
ts最主要的一點就是類型定義,有個概念才好看得懂demo。
vue-property-decorator
這是一個涵蓋了vue的一些對象的集合,我們可以從這里取一些東西出來:
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
取出來的這幾個屬性,分別是 組件定義Component
,父組件傳遞過來的參數(shù)Prop
,原始vue對象Vue
,數(shù)據(jù)監(jiān)聽對象Watch
。還包括這里沒有列舉出來的Model
,Emit
,Inject
,Provide
,可以自己嘗試下。
demo
<template> <div class="hello"> <h2>{{ msg }}--{{ names }}</h2> <input type="text" v-model="txt"> <p>{{ getTxt }}</p> <button @click="add">add</button> <p>{{ sum }}</p> </div> </template> <script lang="ts"> import { Component, Prop, Vue, Watch } from 'vue-property-decorator'; @Component export default class HelloWorld extends Vue { //props @Prop() private msg!: string @Prop() private names!: string //data private txt: string = '1' private sum: number = 0 //computed get getTxt(){ return this.txt } //methods private add(){ this.sum++ console.log(`sum : ${this.sum}`) } //生命周期 created(){ console.log('created') } //watch @Watch('txt') changeTxt(newTxt: string, oldTxt: string){ console.log(`change txt: ${oldTxt} to ${newTxt}`) } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped lang="less"> h4 { margin: 40px 0 0; } input { width: 240px; height: 32px; line-height: 32px; } </style>
以上就是demo,代碼組織有點散,沒有原來js書寫的整齊。
這個demo沒有引入組件,如果需要引入組件,應(yīng)該這樣書寫:
<template> <div class="home"> <img alt="Vue logo" src="../assets/logo.png"> <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" names="aaa" /> <HelloWorld2 msg="Welcome to Your Vue.js + TypeScript App" names="bbb" /> </div> </template> <script lang="ts"> import { Component, Vue } from 'vue-property-decorator'; import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src import HelloWorld2 from '@/components/HelloWorld2.vue'; // @ is an alias to /src @Component({ components: { HelloWorld, HelloWorld2, }, }) export default class Home extends Vue {} </script>
結(jié)語
如果VSCode編輯器有警告提示,比如:
Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
可以把工作區(qū)其他的項目移除,或者把本項目拖動到工作區(qū)的首位,或者在把本項目的tsconfig.json復(fù)制到工作區(qū)首位的項目的根目錄下,重啟編輯器,有比較大的概率可以解決警告提示。
關(guān)于“vue-cli3+typescript的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:vue-cli3+typescript的示例分析-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://aaarwkj.com/article34/dspppe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站制作、軟件開發(fā)、品牌網(wǎng)站制作、響應(yīng)式網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容