ts 模板库文件_vue与ts的使用模版
[源碼地址](https://github.com/jielanglang/simple-vue)
[項(xiàng)目demo](https://xll.netlify.com/)
# 這里講下使用中注意的事項(xiàng)? 具體的使用在項(xiàng)目源碼中
## 關(guān)于typescript詳細(xì)配制
[tsconfig配制詳情](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Compiler%20Options.html)
## 關(guān)于命令
**`npm run creat [paths...]`**
本人對(duì)于不斷的創(chuàng)建組件文件夾已經(jīng)很煩惱所以參考網(wǎng)上 生成了基于node的腳本!
運(yùn)行
```shell
npm run creat views/Home
// or
npm run creat components/Home
```
會(huì)在對(duì)應(yīng)的文件`views`or`components`夾中生成 `index.ts`,`home.html`,`home.scss`
生成的文件內(nèi)部都有基本的使用代碼? 如果對(duì)這個(gè)生成的文件有個(gè)人需求 可以參考 項(xiàng)目目錄下的`generator.js` 文件自行修改
## Vue-Property-Decorator
vue-property-decorator 是在 vue-class-component 上增強(qiáng)了更多的結(jié)合 Vue 特性的裝飾器,新增了這 7 個(gè)裝飾器
- @Emit
- @Inject
- @Model
- @Prop
- @Provide
- @Watch
- @Component (從 vue-class-component 繼承)
## 項(xiàng)目中的語(yǔ)法使用方法
```js
import { Component, Vue } from 'vue-property-decorator'
@Component
export default class App extends Vue {
name:string = 'Simon Zhang'
// computed
get MyName():string {
return `My name is ${this.name}`
}
// methods
sayHello():void {
alert(`Hello ${this.name}`)
}
mounted() {
this.sayHello();
}
}
```
類(lèi)似vue中的
```js
export default {
data () {
return {
name: 'Simon Zhang'
}
},
mounted () {
this.sayHello()
},
computed: {
MyName() {
return `My name is ${this.name}`
}
},
methods: {
sayHello() {
alert(`Hello ${this.name}`)
},
}
}
```
## Vuex-Class
vuex-class是基于基于vue-class-component對(duì)Vuex提供的裝飾器。它的作者同時(shí)也是vue-class-component的主要貢獻(xiàn)者,質(zhì)量還是有保證的。
```
npm i vuex-class -S
```
使用方法
```js
import { Component, Vue } from 'vue-property-decorator'
import { State, Action, Getter } from "vuex-class";
@Component
export default class App extends Vue {
name:string = 'Simon Zhang'
@State login: boolean;
@Action initAjax: () => void;
@Getter load: boolean;
get isLogin(): boolean {
return this.login;
}
mounted() {
this.initAjax();
}
}
```
類(lèi)似vue中
```js
export default {
data() {
return {
name: 'Simon Zhang'
}
},
mounted() {
this.initAjax()
},
computed: {
login() {
return this.$store.state.login
},
load() {
return this.$store.getters.load
}
},
methods: {
initAjax() {
this.$store.dispatch('initAjax')
}
}
}
```
## 支持 mixin
```js
import MixinsType from "@/mixins/xxx";
@Component({
mixins:[MixinsType]
})
```
## 需要注意的事
> 引入部分第三方庫(kù)的時(shí)候需要額外聲明文件
比如說(shuō)我想引入vue-lazyload,雖然已經(jīng)在本地安裝,但是typescript還是提示找不到模塊。原因是typescript是從node_modules/@types目錄下去找模塊聲明,有些庫(kù)并沒(méi)有提供typescript的聲明文件,所以就需要自己去添加
解決辦法:在`src`目錄下建一個(gè)`tools.d.ts`文件,聲明這個(gè)模塊即可
```js
declare module 'vue-awesome-swiper' {
export const swiper: any
export const swiperSlide: any
}
declare module 'vue-lazyload'
```
> 在需要的window屬性的.ts文件 中添加屬性
```js
declare global {
interface Window {FileReader:any}
}
window.FileReader = window.FileReader || {}
```
🔥
總結(jié)
以上是生活随笔為你收集整理的ts 模板库文件_vue与ts的使用模版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于面向用户设计的反思——顶点计划3课程
- 下一篇: 雷电3接口能干嘛_acasis阿卡西斯推