基于 Vite + Vue3 + TS + sass + router + element-plus 的项目搭建

基于 Vite + Vue3 + TS + sass + router + element-plus 的项目搭建

这是一个 Vite + Vue3 + TS 项目且包含 sass、router 和 element-plus

安装 vite

注意:Vite 需要 Node.js 版本 >= 14.6.0
查看 node 版本命令:node -v

查看 vite 版本命令:vite -v

npm

npm create vite@latest

yarn

yarn create vite

创建项目

npm

npm create vite@latest my-vue-app --template vue-ts
// 进入项目目录
cd my-vue-app
// 安装插件
npm install
// 启动
npm run dev

yarn

yarn create vite my-vue-app --template vue-ts
// 进入项目目录
cd my-vue-app
// 安装插件
yarn
// 启动
yarn dev

启动

在这里插入图片描述

sass

安装

npm
npm install sass sass-loader -D
yarn
yarn add sass sass-loader -D

使用

注意:about.vue 文件中 lang=“scss”

  • Scss是从 Sass3 引入进来的,scss语法有"{}“,”;"而sass没有,所以sass-loader对他们的解析是不一样的;

  • Sass从第三代开始,放弃了缩进式风格,并且完全向下兼容普通的CSS代码,这一代的Sass也被称为Scss

  • 如果使用 vscode 开发,建议安装插件 Live Sass Compiler

详细语法,参考此篇

<template>
    <div>
        <h1>About</h1>
    </div>
</template>
<style lang="scss">
h1 {
    color: red;
}
</style>

效果

在这里插入图片描述

router

安装

npm
npm install vue-router@4
yarn
yarn add vue-router@4

配置

这里只是简单使用,参考此处

src 目录下

  • 新建 router/index.ts 文件;
  • 新建 views 文件夹,放置路由页,如新建 home.vue 和 about.vue;

home.vue

<template>
    <div>
        <h1>Home</h1>
    </div>
</template>

about.vue

<template>
    <div>
        <h1>About</h1>
    </div>
</template>

router/index.ts

import { createRouter, createWebHashHistory } from 'vue-router'
import home from '../views/home.vue'
import about from '../views/about.vue'

// 定义路由
const routes = [
    {
        path: '/home', component: home
    },
    {
        path: '/about', component: about
    }
];

// 创建路由实例并传递 `routes` 配置
const router = createRouter({
    history: createWebHashHistory(),
    routes
});

export default router

main.ts 中挂载根实例

import { createApp } from 'vue'
import App from './App.vue'
import router from 'router/index.ts'

createApp(App)
.use(router) // 挂载到根实例
.mount('#app')

改造 App.vue

<template> 
  <!--使用 router-link 组件进行导航。通过传递 `to` 来指定链接 -->
  <router-link to="/home">Go Home</router-link><br/>
  <router-link to="/about">Go About</router-link>
  <!-- 路由匹配到的组件将渲染在这里 -->
  <router-view/>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

效果

在这里插入图片描述

element-plus

安装

官网在此

npm

npm install element-plus --save

yarn

yarn add element-plus

引入

main.ts 引入,仅全局引入,按需导入在此

import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index.ts'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

createApp(App)
.use(router) // 挂载到根实例
.use(ElementPlus)
.mount('#app')

Volar 支持
如果您使用 Volar,请在 tsconfig.json 中通过 compilerOptions.type 指定全局组件类型
tsconfig.json

{
  "compilerOptions": {
    // ...
    "types": ["element-plus/global"]
  }
}

使用

src/views/home.vue

<template>
    <div>
        <h1>Home</h1>
        <el-button type="primary">测试按钮</el-button>
    </div>
</template>

效果

在这里插入图片描述

其它

如果想使用 vue cli 创建项目,可参考《基于 Vue Cli4.x + Vue3 + TS + stylus +router + element-plus 的项目搭建》

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于Vue3、ViteTypeScript项目搭建步骤: 1. 安装 Node.js 和 npm 在官网下载并安装 Node.js,npm会随之安装。 2. 创建项目 打开命令行工具,使用以下命令创建一个新的 Vue3 项目: ``` npm init vite-app <project-name> cd <project-name> npm install ``` 3. 添加 TypeScript 支持 通过以下命令安装 TypeScript: ``` npm install --save-dev typescript ``` 然后在项目根目录下创建一个 `tsconfig.json` 文件: ```json { "compilerOptions": { "target": "es6", "module": "esnext", "strict": true, "jsx": "preserve", "importHelpers": true, "moduleResolution": "node", "experimentalDecorators": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "sourceMap": true, "baseUrl": ".", "paths": { "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"] }, "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"] } ``` 4. 添加 Vue RouterVuex 通过以下命令安装 Vue RouterVuex: ``` npm install vue-router vuex --save ``` 5. 添加 SCSS 支持 通过以下命令安装 SCSS: ``` npm install sass sass-loader fibers --save-dev ``` 在 `src` 目录下创建一个名为 `style.scss` 的文件,并在 `main.ts` 中导入: ```scss @import './style.scss'; ``` 6. 添加 ESLint 和 Prettier 通过以下命令安装 ESLint 和 Prettier: ``` npm install eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier eslint-config-prettier eslint-plugin-prettier --save-dev ``` 在项目根目录下创建一个 `.eslintrc.js` 文件: ```js module.exports = { root: true, env: { node: true, }, extends: [ "plugin:vue/vue3-essential", "@vue/typescript/recommended", "prettier", "prettier/@typescript-eslint", "prettier/vue", ], parserOptions: { ecmaVersion: 2020, }, rules: { "no-console": process.env.NODE_ENV === "production" ? "error" : "off", "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off", "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/explicit-module-boundary-types": "off", }, }; ``` 在项目根目录下创建一个 `.prettierrc.js` 文件: ```js module.exports = { semi: true, trailingComma: "all", singleQuote: true, printWidth: 120, tabWidth: 2, }; ``` 7. 添加 Axios 支持 通过以下命令安装 Axios: ``` npm install axios --save ``` 在 `src` 目录下创建一个名为 `api.ts` 的文件,并在其中定义一个 Axios 实例: ```typescript import axios from 'axios'; const api = axios.create({ baseURL: process.env.VUE_APP_API_BASE_URL, timeout: 30000, }); export default api; ``` 8. 添加测试支持 通过以下命令安装 Jest 和 Vue Test Utils: ``` npm install jest @vue/test-utils ts-jest vue-jest babel-jest --save-dev ``` 在项目根目录下创建一个 `jest.config.js` 文件: ```js module.exports = { preset: "ts-jest", testEnvironment: "jsdom", transform: { "^.+\\.vue$": "vue-jest", "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest", }, moduleNameMapper: { "^@/(.*)$": "<rootDir>/src/$1", }, setupFilesAfterEnv: ["<rootDir>/tests/setup.ts"], collectCoverage: true, collectCoverageFrom: ["src/**/*.{ts,vue}", "!**/node_modules/**"], coverageReporters: ["text-summary", "lcov", "html"], }; ``` 在项目根目录下创建一个 `tests/setup.ts` 文件: ```typescript import { config } from "@vue/test-utils"; config.global.mocks.$t = (key: string) => key; ``` 9. 运行项目 使用以下命令启动项目: ``` npm run dev ``` 此时,你已经成功搭建了一个基于 Vue3、ViteTypeScript 的项目!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值