侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

用Vue+TypeScript项目配置实战

前端  /  管理员 发布于 6年前   294

下面由js教程栏目给大家介绍如何用Vue+TypeScript项目配置实战,希望对需要的朋友有所帮助!

用Vue+TypeScript项目配置实战

项目搭建

通过脚手架搭建

1. 通过Vue CLI 3 创建vue项目

vue create vue-typescript// 在此选择typescript支持? Check the features needed for your project: () Babel () TypeScript ( ) Progressive Web App (PWA) Support () Router () Vuex >() CSS Pre-processors () Linter / Formatter ( ) Unit Testing ( ) E2E Testing 我是08年出道的高级前端架构师,有问题或者交流经验可以进我的扣扣裙 519293536 我都会尽力帮大家哦
// 引入 vue-class-component 插件 // 以下大概是我的选择 ? Use class-style component syntax? (Y/n) y ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with node-sass) ? Pick a linter / formatter config: Prettier ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ? Save this as a preset for future projects? (y/N) n

相关推荐:《js高级教程》

2. 启动项目

yarn run serve

能跑起来吗,能跑就是好项目

3.项目配置

此时其实脚手架已经帮我们配置好了大多数的配置,但还是需要熟悉一下配置。

tsconfig.json

在项目根目录下创建tsconfig.json。

{  // 编译选项  "compilerOptions": {    // 输出目录    "outDir": "./output",    // 是否包含可以用于 debug 的 sourceMap    "sourceMap": true,    // 以严格模式解析    "strict": true,    // 采用的模块系统    "module": "esnext",    // 如何处理模块    "moduleResolution": "node",    // 编译输出目标 ES 版本    "target": "es5",    // 允许从没有设置默认导出的模块中默认导入    "allowSyntheticDefaultImports": true,    // 将每个文件作为单独的模块    "isolatedModules": false,    // 启用装饰器    "experimentalDecorators": true,    // 启用设计类型元数据(用于反射)    "emitDecoratorMetadata": true,    // 在表达式和声明上有隐含的any类型时报错    "noImplicitAny": false,    // 不是函数的所有返回路径都有返回值时报错。    "noImplicitReturns": true,    // 从 tslib 导入外部帮助库: 比如__extends,__rest等    "importHelpers": true,    // 编译过程中打印文件名    "listFiles": true,    // 移除注释    "removeComments": true,    "suppressImplicitAnyIndexErrors": true,    // 允许编译javascript文件    "allowJs": true,    // 解析非相对模块名的基准目录    "baseUrl": "./",    // 指定特殊模块的路径    "paths": {      "jquery": [        "node_modules/jquery/dist/jquery"      ]    },    // 编译过程中需要引入的库文件的列表    "lib": [      "dom",      "es2015",      "es2015.promise"    ]  }}

tslint.json

在根路径下创建tslint.json文件,就是 引入 ts 的 standard 规范。

如果已经引入了eslint的配置文件,这一步其实也可以不做。

{  "extends": "tslint-config-standard",  "globals": {    "require": true  }}

复制代码

附上一个脚手架自动生成的eslint的默认配置 eslintrc.js。

module.exports = {  root: true,  env: {    node: true  },  extends: [    "plugin:vue/essential",    "eslint:recommended",    "@vue/typescript/recommended",    "@vue/prettier",    "@vue/prettier/@typescript-eslint"  ],  parserOptions: {    ecmaVersion: 2020  },  rules: {    "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",    "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off"  }};

4.支持ES6 / ES7

在 tsconfig.json中,添加对es6 / es7的支持,更多的配置请见tsconfig - 编译选项。

"lib": [    "dom",    "es5",    "es6",    "es7",    "es2015.promise"]

5.配置Vuex

首先当然是先安装依赖啦。

yarn add vuex vuex-class

复制代码

vuex:在 vue 中集中管理应用状态

vuex-class :在 vue-class-component 写法中 绑定 vuex。

引用了vuex-class之后还是和原来的写法有点儿区别的。

vuex store 中该怎么写,还怎么写,引用的时候如下:

import { Component, Prop, Vue } from "vue-property-decorator";import { State, Getter, Action, Mutation, namespace } from "vuex-class";// 声明使用的是哪个模块const commonModule = namespace("common");@Componentexport default class HelloWorld extends Vue {  @Prop() private msg!: string;  // 获取vuex中的数据  @commonModule.State("token") token!: string;  @commonModule.Getter("getToken") getToken!: string;  @commonModule.Action("setToken") actionSetToken: (arg0: string) => void;  @commonModule.Mutation("setToken") mutationSetToken: unknown;  // @State token  // @Getter("token") getterToken;  // @Action("token") actionToken;  // @Mutation("token") mutationToken;  created() {    this.actionSetToken("123");    alert(this.token);  }}

6.支持 vue mixin 函数

在src下新建mixins目录,根据业务复用模块划分结构。

在使用Vue进行开发时我们经常要用到混合,结合TypeScript之后一般有两种mixins的方法。

一种是vue-property-decorator提供的

// 定义 routerMixins.ts文件// mixin router 公共方法import Vue from 'vue'import Component from 'vue-class-component'@Componentexport default class myMixins extends Vue {  /**   * @author: WangXinYu   * @describe: 浏览器后退事件   * @param: {}   * @return:   **/  goBack() {    this.$router.go(-1)  }  /**   * @author: WangXinYu   * @describe: test   * @param: {}   * @return:   **/  routerTest() {    console.log('are you ok?')  }}
// 引入 mixinimport { Component, Prop, Vue, Mixins } from 'vue-property-decorator'import routerMixins from '@/mixins/router'@Componentexport default class HelloWorld extends Mixins(routerMixins) {  created() {    this.routerTest()  }}

第二种是在@Component中混入。

// mixins.tsimport { Vue, Component } from 'vue-property-decorator';declare module 'vue/types/vue' {    interface Vue {        value: string;    }}@Componentexport default class myMixins extends Vue {    value: string = 'Hello'}
// 混入import { Vue, Component, Prop } from 'vue-property-decorator';import myMixins from '@/mixins/myMixins';@Component({    mixins: [myMixins]})export default class myComponent extends Vue{    created(){        console.log(this.value) // => Hello    }}

都会了吗?如果不懂 ,记住我 。我是08年出道的高级前端架构师,有问题或者交流经验可以进我的扣扣裙 519293536 我都会尽力帮大家哦

本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理

(未完待续...)

以上就是用Vue+TypeScript项目配置实战的详细内容,更多请关注其它相关文章!


  • 上一条:
    为什么vue不使用ajax
    下一条:
    详解layuiAdmin单页版根据后台json数据动态生成左侧菜单栏
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客