国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

配置Vite+React+TS項(xiàng)目

這篇具有很好參考價(jià)值的文章主要介紹了配置Vite+React+TS項(xiàng)目。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

初始化

執(zhí)行npm create vite并填寫項(xiàng)目名、用那個(gè)框架。。

配置

路徑別名

vite.config.ts里面配置:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, './src') // 路徑別名
      ...
    }
  }
})

如果開發(fā)環(huán)境是ts,會(huì)提示如找不到path或找不到__dirname等,需要先安裝一下node的類型聲明文件:
npm i -D @types/node

然后去修改根目錄的tsconfig.json文件,不然你使用路徑別名引入組件的時(shí)候會(huì)報(bào)錯(cuò),沒寫baseUrl vite會(huì)warning:

{
  "compilerOptions": {
    "baseUrl": "./",
    /* path alias */
    "paths": {
      "@/*": ["src/*"],
      ...
    }
  },
}

配置Less

直接執(zhí)行npm i less就行

路由

react-router-dom是處理項(xiàng)目中路由問題的組件庫(kù),默認(rèn)是沒有這個(gè)組件庫(kù),需要額外下載:
npm add react-router-dom --save-dev
在根目錄下的index.tsx里面把<App />外層包裹上:

import { BrowserRouter } from "react-router-dom";
import ReactDOM from 'react-dom/client';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <BrowserRouter>
      <App />	
  </BrowserRouter>
);

自己去src里面創(chuàng)建router文件夾處理路由的信息,然后在App.tsx里面加上useRoutes:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-834180.html

import { Suspense } from 'react';
import { useRoutes } from 'react-router-dom'
import './App.css';
import routes from "./router"; // router文件夾的路由信息

function App() {
  return (
    <div className="App">
      <Suspense fallback="loading">
        <div className="main">
          {useRoutes(routes)}
        </div>
      </Suspense>
    </div>
  );
}

export default App;

封裝Axios

import axios, { AxiosRequestConfig } from 'axios';

//默認(rèn)請(qǐng)求超時(shí)時(shí)間
const timeout = 30000;

//創(chuàng)建axios實(shí)例
const service = axios.create({
  timeout,
  // 如需要攜帶cookie 該值需設(shè)為true
  withCredentials: true,
  // headers:{
  //   Authorization:"Basic YWRtaW46dGVzdA=="
  // }
});

// function getCookie(cookieName:any) {
//   const name = cookieName + "=";
//   const decodedCookie = decodeURIComponent(document.cookie);
//   const cookieArray = decodedCookie.split(';');
//   for (let i = 0; i < cookieArray.length; i++) {
//     let cookie = cookieArray[i];
//     while (cookie.charAt(0) === ' ') {
//       cookie = cookie.substring(1);
//     }
//     if (cookie.indexOf(name) === 0) {
//       return cookie.substring(name.length, cookie.length);
//     }
//   }
//   return "";
// }

// const yourCookieValue = getCookie("FLOWABLE_REMEMBER_ME");

// 統(tǒng)一請(qǐng)求攔截 可配置自定義headers 例如 language、token等
service.interceptors.request.use(
  (config: any) => {
    
    // if(config.url.includes("/workFlow")){
      
      
    //   config.headers.cookie = 'FLOWABLE_REMEMBER_ME' + yourCookieValue
    //   console.log(config.headers);
    // }
    return config
  },
  error => {
    console.log(error)
    Promise.reject(error)
  }
)

// axios返回格式
interface axiosTypes<T>{
  data: T;
  status: number;
  statusText: string;
}

// axios.create()

// 后臺(tái)響應(yīng)數(shù)據(jù)格式
// interface responseTypes<T>{
//   code: number,
//   msg: string,
//   result: T
// }

//核心處理代碼 將返回一個(gè)promise 調(diào)用then將可獲取響應(yīng)的業(yè)務(wù)數(shù)據(jù)
const requestHandler = <T>(method: 'get' | 'post' | 'put' | 'delete' | 'patch', url: string, params: object = {}, config: AxiosRequestConfig = {}): Promise<T> => {
  //   let response: Promise<axiosTypes<responseTypes<T>>>;
  let response: Promise<axiosTypes<any>>;
  // if(url.includes('/flowable')) config = {
  //   headers:{
  //   'Cookie': 'FLOWABLE_REMEMBER_ME' + yourCookieValue
  // }}
  switch(method){
    case 'get':
      response = service.get(url, {params: { ...params }, ...config});
      break;
    case 'post':
      response = service.post(url, params, {...config});
      break;
    case 'put':
      response = service.put(url, {...params}, {...config});
      break;
    case 'delete':
      response = service.delete(url, {params: { ...params }, ...config});
      break
    case 'patch':
      response = service.patch(url, {...params}, {...config});
      break;
  }
  
  return new Promise<T>((resolve, reject) => {
    response.then(res => {
      // 業(yè)務(wù)代碼 可根據(jù)需求自行處理
      const data = res.data;
      
      if(res.status !== 200 && res.status !== 304){
        
        // 特定狀態(tài)碼 處理特定的需求
        if(data.code == 401){
          console.log('登錄異常,執(zhí)行登出...');
        }
        const e: string = JSON.stringify(data);
        console.log(`請(qǐng)求錯(cuò)誤:${e}`)
        reject(data);
      }else{
        // 數(shù)據(jù)請(qǐng)求正確 使用 resolve 將結(jié)果返回
        resolve(res.data);
      }
    }).catch(error => {
      const e: string = JSON.stringify(error);
      console.log(`網(wǎng)絡(luò)錯(cuò)誤:${e}`)
      reject(error);
    })
  })
}

// 使用 request 統(tǒng)一調(diào)用,包括封裝的get、post、put、delete等方法
const request = {
  get: <T>(url: string, params?: object, config?: any) => requestHandler<T>('get', url, params, config),
  post: <T>(url: string, params?: object, config?: AxiosRequestConfig) => requestHandler<T>('post', url, params, config),
  put: <T>(url: string, params?: object, config?: AxiosRequestConfig) => requestHandler<T>('put', url, params, config),
  delete: <T>(url: string, params?: object, config?: AxiosRequestConfig) => requestHandler<T>('delete', url, params, config),
  patch: <T>(url: string, params?: object, config?: AxiosRequestConfig) => requestHandler<T>('patch', url, params, config),
};

// 導(dǎo)出至外層,方便統(tǒng)一使用
export { request };

到了這里,關(guān)于配置Vite+React+TS項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 搭建React項(xiàng)目,基于Vite+React+TS+ESLint+Prettier+Husky+Commitlint

    搭建React項(xiàng)目,基于Vite+React+TS+ESLint+Prettier+Husky+Commitlint

    node: 20.10.0 安裝包管理器pnpm 基于Vite創(chuàng)建項(xiàng)目 進(jìn)入項(xiàng)目目錄安裝依賴 啟動(dòng)項(xiàng)目 1、初始化ESLint 按下圖結(jié)果進(jìn)行初始化選擇: 按需選擇完配置后,選擇立即安裝,就可一鍵安裝相關(guān)依賴。安裝成功后 ESLint 幫我們創(chuàng)建了 .eslintrc.cjs 配置文件(cjs 是指 CommonJS 格式)。 2、在項(xiàng)目

    2024年02月04日
    瀏覽(27)
  • 使用vite創(chuàng)建Vue/React前端項(xiàng)目,配置@別名和Sass樣式,又快又方便

    使用vite創(chuàng)建Vue/React前端項(xiàng)目,配置@別名和Sass樣式,又快又方便

    Vite官方網(wǎng)站:Vite | 下一代的前端工具鏈? Vite 并不是基于 Webpack 的,它有自己的開發(fā)服務(wù)器,利用瀏覽器中的原生 ES 模塊。這種架構(gòu)使得 Vite 比 Webpack 的開發(fā)服務(wù)器快了好幾個(gè)數(shù)量級(jí)。Vite 采用 Rollup 進(jìn)行構(gòu)建,速度也更快。? 兼容性注意 Vite 需要?Node.js?版本 14.18+,16+。然

    2024年02月14日
    瀏覽(50)
  • 【React】TS項(xiàng)目配置Redux

    【React】TS項(xiàng)目配置Redux

    在React中使用Redux,官方要求安裝兩個(gè)插件, Redux Toolkit 和 react-redux Redux Toolkit (RTK): 官方推薦編寫Redux邏輯的方式,是一套工具的集合集, 簡(jiǎn)化書寫方式 。 簡(jiǎn)化 store 的配置方式 內(nèi)置 immer 支持可變式狀態(tài)修改 內(nèi)置 thunk 更好的異步創(chuàng)建 react-redux :用來(lái) 鏈接 Redux 和 React組

    2024年01月22日
    瀏覽(27)
  • 【實(shí)戰(zhàn)】 TS 應(yīng)用:JS神助攻 - 強(qiáng)類型 —— React17+React Hook+TS4 最佳實(shí)踐,仿 Jira 企業(yè)級(jí)項(xiàng)目(三)

    學(xué)習(xí)內(nèi)容來(lái)源:React + React Hook + TS 最佳實(shí)踐-慕課網(wǎng) 相對(duì)原教程,我在學(xué)習(xí)開始時(shí)(2023.03)采用的是當(dāng)前最新版本: 項(xiàng) 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月09日
    瀏覽(26)
  • 【實(shí)戰(zhàn)】三、TS 應(yīng)用:JS神助攻 - 強(qiáng)類型 —— React17+React Hook+TS4 最佳實(shí)踐,仿 Jira 企業(yè)級(jí)項(xiàng)目(三)

    學(xué)習(xí)內(nèi)容來(lái)源:React + React Hook + TS 最佳實(shí)踐-慕課網(wǎng) 相對(duì)原教程,我在學(xué)習(xí)開始時(shí)(2023.03)采用的是當(dāng)前最新版本: 項(xiàng) 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月11日
    瀏覽(33)
  • react+ts【項(xiàng)目實(shí)戰(zhàn)一】配置項(xiàng)目/路由/redux

    react+ts【項(xiàng)目實(shí)戰(zhàn)一】配置項(xiàng)目/路由/redux

    1、該項(xiàng)目使用的是ts創(chuàng)建的 所以需要加上 --template typescript create-react-app kiki_ts_react_music --template typescript 2、 整理項(xiàng)目結(jié)構(gòu) 刪除一些自己用不到的文件 1.2.1 更換icon 1.2.2 更換項(xiàng)目名稱 在index.html文件里面 1.2.1 配置項(xiàng)目別名 1、 npm i -D @craco/craco 2、在根文件創(chuàng)建 craco.config.ts 3、修

    2024年02月19日
    瀏覽(26)
  • React18TS項(xiàng)目:配置react-css-modules,使用styleName

    React18TS項(xiàng)目:配置react-css-modules,使用styleName

    他的好處不說了 網(wǎng)上一堆文章一個(gè)能打的都沒有, 添加開發(fā)依賴 Babel Plugin \\\"React CSS Modules\\\" | Dr. Pogodin Studio 看@dr.pogodin/babel-plugin-react-css-modules官方文檔 不使用babel-plugin-react-css-modules 手搭webpack配置需要處理 1.能啟用css modules 對(duì)于裸 Webpack,請(qǐng)參見webpack css-loader的 modules 的選項(xiàng)

    2024年02月12日
    瀏覽(29)
  • React18入門(第二篇)——React18+Ts項(xiàng)目配置husky、eslint、pretttier、commitLint

    React18入門(第二篇)——React18+Ts項(xiàng)目配置husky、eslint、pretttier、commitLint

    我的項(xiàng)目版本如下: React: V18.2.0 Node.js: V16.14.0 TypeScript:最新版 工具: VsCode 本文將采用圖文詳解的方式,手把手帶你快速完成在React項(xiàng)目中配置husky、prettier、commitLint,實(shí)現(xiàn)編碼規(guī)范的統(tǒng)一,git提交規(guī)范的統(tǒng)一。 1.1 裝包 1.2 ESLint 插件安裝 1.3 創(chuàng)建命令并使用 新增命令 執(zhí)行

    2024年02月08日
    瀏覽(29)
  • 【實(shí)戰(zhàn)】 項(xiàng)目起航:項(xiàng)目初始化與配置 —— React17+React Hook+TS4 最佳實(shí)踐,仿 Jira 企業(yè)級(jí)項(xiàng)目(一)

    學(xué)習(xí)內(nèi)容來(lái)源:React + React Hook + TS 最佳實(shí)踐-慕課網(wǎng) 相對(duì)原教程,我在學(xué)習(xí)開始時(shí)(2023.03)采用的是當(dāng)前最新版本: 項(xiàng) 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月10日
    瀏覽(19)
  • react+vue 前端國(guó)密算法sm2、sm3 、sm4的js ts實(shí)現(xiàn)

    1. 簡(jiǎn)單介紹下SM2 和 SM3 SM2 算法:是一種公鑰加密算法,它的密鑰長(zhǎng)度為 256 位,安全性較高??捎糜跀?shù)字簽名、密鑰協(xié)商等場(chǎng)景。 SM3 算法:是一種對(duì)稱加密算法,用于消息摘要和數(shù)字簽名等場(chǎng)景。它的密鑰長(zhǎng)度為 256 位,安全性較高。SM3 算法與 SM2 算法相互配合,提高了整體

    2024年01月19日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包