最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Vue3 + Vite + TypeScript 项目实战 -----(一)搭建项目

    正文概述 掘金(SoulReader)   2021-02-13   1294

    Vue3 新版出来了这么久,虽然之前也有联系过搭建,但是并没有测试过多的东西,趁着闲暇时间,打算从头采坑,记录自己的 Vue 3.0 的爬坑历程

    搭建项目

    本次项目搭建采用尤雨溪大大开发的 Vite,这是尤大最近开发出的新的 Web 开发工具具有以下优点

    • 快速的冷启动
    • 即时的模块热更新
    • 真正的按需编译

    使用 vite 极大的提高了前端的开发性能及开发速度

    全局安装 Vite

      // 全局安装 vite-app
      npm i -g vite-app
    
      // 创建项目
      npm init vite-app <project-name>
    
      cd project-name
    
      yarn || npm install
    

    安装 必要第三方插件

    安装 TypeScript

      npm install -D typescript
    

    根目录(src)下新增 shim.vue.d.ts 文件

      declare module '*.vue' {
        import { Component } from 'vue'
        const component: Component
        export default component
      }
    
      // 或者
      declare module '*.vue' {
        import Vue from 'vue'
        // const component: defineComponent<{},{},any>
        export default Vue
      }
    

    修改 main.js 为 main.ts

    并修改 index.html 文的引用

    安装 vue-router

    vue3.0 最好安装最新的版的 vue-router,版本错误的话无法使用路由进行跳转

      npm install vue-router@4
    

    然后在 src 目录下新建 router 目录,在目录下新建 index.ts 文件

    从 vue-router 引入 createRoutercreateWebHashHistory(或者createWebHistory)

      import {createRouter, createWebHashHistory} from 'vue-router'
    

    然后新建 一个 route对象,存放路由配置,使用 createRouter 方法 创建 router 对象,最后通过 export default 导出

      const routes = [
        {
          path: '/',
          component: () => import('../views/home/index.vue'),
          redirect: '/index',
          children: [
            {
              path: '/login',
              component: () => import('../views/login/index.vue')
            },
          ]
        },
      ]
    
      var router = createRouter({
        history: createWebHashHistory(),
        routes
      })
    
      export default router
    

    在 mian.ts 对象中 通过 import 引入,并用 Vue 的 use 方法 注册到 Vue 实例上

      import router from './router'
      createApp(App).use(router).mount('#app')
    

    使用 Vue 状态管理工具 Vuex

      // 安装
      npm install vuex@next
    
      // 使用
      // /src/store/index.ts
      import Vuex from 'vuex'
      const store = new Vuex.Store({
        // ...
        modules: {},
        state: () => {
          return {
            name: 'lxx'
          }
        },
        mutations: {},
        actions: {},
        getters: {}
      })
    
      export default store
    
      // 注册
      import store from './store'
      createApp(App).use(router).use(store).mount('#app')
    

    使用 sass 语法

      // 安装 sass
      yarn add sass
    
      // 安装完成之后 将 sass 从 dependencies 移动到 devDependencies
      // 使用时 在 style 后加 lang="scss"
    

    起源地下载网 » Vue3 + Vite + TypeScript 项目实战 -----(一)搭建项目

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元