vue-cli3 + ts 环境搭建

vue-cli3 + ts 环境搭建

技术栈

- typescript
- vue-cli 3.x+
- yarn
- sass
- element-ui
- vuex

配置

    1. npm install -g @vue/cli(版本 vue-cli3)
        # OR
        yarn global add @vue/cli
    
    2. vue create project-name
        # OR
        vue ui
    
    选配
    Please pick a preset:
        -> Manually select features
            -> Babel | TypeScript | Router | Vuex  | Css Pre-processors | Liner / Formatter
    
    校验
    Pick a linter / formatter config
        -> TSLint
    
    保存的时校验还是提交时校验,选择保存时
    Pink additional lint features
        ->Lint on save
    
    单独存放还是集成在package.json,选择单独存放
    Where do you prefer placing config for Babel, PstCSS, ESLint, etc?
        -> In dedicated config files
    
    将此保存为将来项目的预设
    Save this as a preset for future projects ? 
        -> NO
        
    运行
    yarn serve

目录

    public
        - index.html
    src
        - assets: 静态资源
        - components:公共组件
        - router:路由
        - store:数据
        - views:容器组件
        - App.vue:根组件
        - main.ts:入口
        - shims-tsx.d.ts
        - shims-vue.d.ts
        
    .browserslistrc:项目的目标浏览器的范围,会被 @babel/preset-env 和 Autoprefixer 
                    用来确定需要转译的 JavaScript 特性和需要添加的 CSS 浏览器前缀。
    .gitignore
    babel.config.js:ES6语法编译配置
    package.json:项目及工具的依赖配置文件
    tsconfig.json:ts的配置文件
    tsline.json

cli3 安装后已有插件

  • vue-class-component:使用 TypeScript装饰器增强 Vue 组件
  • vue-property-decorator:增强更多的结合 Vue 特性的装饰器
  • vue-router
  • vuex
  • node-sass
  • sass-loader
  • typescript

element-ui

    全部引入:
        import ElementUI from 'element-ui';
        import 'element-ui/lib/theme-chalk/index.css';
        Vue.use(ElementUI);
        
    按需引入:
        - 插件:yarn add babel-plugin-component -D
        - .babelrc
          "plugins": [
            [
              "component",
              {
                "libraryName": "element-ui",
                "styleLibraryName": "theme-chalk"
              }
            ]
          ]
        - 引入:
            import { Button, Select } from 'element-ui';
            Vue.use(Button).use(Select)

babel.config.js

    yarn add @vue/babel-preset-app -D
    
     presets: [
        '@vue/app',
        {
          polyfills: [
            'es6.promise',
            'es6.symbol'
          ]
        }
      ],
      
    err: Module build failed (from ./node_modules/babel-loader/lib/index.js)
        yarn add @babel/core @babel/preset-env -D
    
    

vue-loader

   解决:Cannot find module .vue
   https://vue-loader.vuejs.org/zh/guide/
   npm install vue-loader -D
   
   根目录新建vue.config.js:
        const VueLoaderPlugin = require('vue-loader/lib/plugin')
        
        module.exports = {
          module: {
            rules: [
              // ... 其它规则
              {
                test: /\.vue$/,
                loader: 'vue-loader'
              }
            ]
          },
          plugins: [
            // 请确保引入这个插件!
            new VueLoaderPlugin()
          ]
        }

vueRouter

    import Router, { RouteConfig } from 'vue-router';
    
    - RouteConfig的类型定义:
        interface RouteConfig = {
          path: string; //路径
          component?: Component;
          name?: string; // 命名路由
          components?: { [name: string]: Component }; // 命名视图组件
          redirect?: string | Location | Function; //重定向
          props?: boolean | Object | Function;
          alias?: string | Array<string>; //别名
          children?: Array<RouteConfig>; // 嵌套路由
          beforeEnter?: (to: Route, from: Route, next: Function) => void;
          meta?: any; //路由元信息 使用$route.meta.属性可以获取
        
          // 2.6.0+
          caseSensitive?: boolean; // 匹配规则是否大小写敏感?(默认值:false)
          pathToRegexpOptions?: Object; // 编译正则的选项
        }

tslint 规则 :去掉tslint规则 - 删除"extends": [// “tslint:recommended”]即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值