這篇文章將為大家詳細(xì)講解有關(guān)vue-cli3+typescript的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
我們提供的服務(wù)有:做網(wǎng)站、網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、和龍ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的和龍網(wǎng)站制作公司
vue-cli3
vue-cli3的詳細(xì)功能推薦官方文檔,不在本文介紹范圍內(nèi)。
安裝:
npm install -g @vue/cli
檢查安裝成功與否:
vue --version
創(chuàng)建項(xiàng)目:
vue create myapp
可以選擇Manually select feature
來自由選擇功能,常用的有vuex、vue-router、CSS Pre-processors等,我們再把typescript勾上,就可以回車進(jìn)入下一步了。PS:勾選的操作是按空格鍵。
創(chuàng)建成功之后,執(zhí)行啟動(dòng)命令:
npm run serve
就可以通過http://localhost:8080/
訪問本地項(xiàng)目啦。
typescript
如果沒有typescript基礎(chǔ),可以先補(bǔ)補(bǔ)課,大概花三十分鐘就可以了解typescript的一些特性,比如:TypeScript 入門教程。
ts最主要的一點(diǎn)就是類型定義,有個(gè)概念才好看得懂demo。
vue-property-decorator
這是一個(gè)涵蓋了vue的一些對(duì)象的集合,我們可以從這里取一些東西出來:
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
取出來的這幾個(gè)屬性,分別是 組件定義Component
,父組件傳遞過來的參數(shù)Prop
,原始vue對(duì)象Vue
,數(shù)據(jù)監(jiān)聽對(duì)象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,代碼組織有點(diǎn)散,沒有原來js書寫的整齊。
這個(gè)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ū)其他的項(xiàng)目移除,或者把本項(xiàng)目拖動(dòng)到工作區(qū)的首位,或者在把本項(xiàng)目的tsconfig.json復(fù)制到工作區(qū)首位的項(xiàng)目的根目錄下,重啟編輯器,有比較大的概率可以解決警告提示。
關(guān)于“vue-cli3+typescript的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)站欄目:vue-cli3+typescript的示例分析
鏈接URL:http://chinadenli.net/article32/ppccpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、外貿(mào)建站、ChatGPT、自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)