vue-cli3+typescript初體驗(yàn)小結(jié)
前言
氣勢(shì)洶涌,ts似乎已經(jīng)在來的路上,隨時(shí)可能敲門。
2015年,三大前端框架開始火爆的時(shí)候,我還在抱著Backbone不放,一直覺得可以輕易轉(zhuǎn)到其他框架去。后來?yè)Q工作,現(xiàn)實(shí)把臉都打腫了,沒做過vue、react、angular?不要!
今天,不能犯這個(gè)錯(cuò)了,畢竟時(shí)不我與,都快奔三了。
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等,我們?cè)侔裻ypescript勾上,就可以回車進(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"> <h1>{{ msg }}--{{ names }}</h1> <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"> h3 { 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)目的根目錄下,重啟編輯器,有比較大的概率可以解決警告提示。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中進(jìn)行分布式鑒權(quán)與認(rèn)證的過程
在Vue應(yīng)用中,我們通常需要實(shí)現(xiàn)分布式鑒權(quán)和認(rèn)證,以確保用戶的安全性和數(shù)據(jù)的保密性,本文將介紹在Vue中如何進(jìn)行分布式鑒權(quán)與認(rèn)證,需要的朋友可以參考下2023-06-06elementUi vue el-radio 監(jiān)聽選中變化的實(shí)例代碼
這篇文章主要介紹了elementUi vue el-radio 監(jiān)聽選中變化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Vue 中使用vue2-highcharts實(shí)現(xiàn)top功能的示例
下面小編就為大家分享一篇Vue 中使用vue2-highcharts實(shí)現(xiàn)top功能的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03詳解Vue3?SFC?和?TSX?方式調(diào)用子組件中的函數(shù)
在使用?.vue?定義的組件中,setup?中提供了?defineExpose()?方法,該方法可以將組件內(nèi)部的方法暴露給父組件,這篇文章主要介紹了Vue3?SFC?和?TSX?方式調(diào)用子組件中的函數(shù),需要的朋友可以參考下2022-10-10Vue項(xiàng)目中常用的工具函數(shù)總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中常用的工具函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09vue的diff算法知識(shí)點(diǎn)總結(jié)
本篇文章給大家分享了關(guān)于vue的diff算法的相關(guān)知識(shí)點(diǎn)總結(jié),有興趣的朋友參考學(xué)習(xí)下。2018-03-03vue項(xiàng)目退出登錄清除store數(shù)據(jù)的三種方法
最近使用vue做用戶的登錄/退出,在開發(fā)過程中遇到的一些問題,記錄下來,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目退出登錄清除store數(shù)據(jù)的三種方法,需要的朋友可以參考下2022-09-09在vscode中統(tǒng)一vue編碼風(fēng)格的方法
本篇文章主要介紹了在vscode中統(tǒng)一vue編碼風(fēng)格的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02