赞
踩
原链接:Vue3.0+typescript+Vite+Pinia+Element-plus搭建vue3框架!
# npm 6.x
npm init vite@latest my-vue-app --template vue
# npm 7+, 需要额外的双横线:
npm init vite@latest my-vue-app -- --template vue
# yarn
yarn create vite my-vue-app --template vue
# pnpm
pnpm create vite my-vue-app -- --template vue
# cd进入my-vue-app项目文件夹
cd my-vue-app
# 安装依赖
yarn
# 运行项目
yarn dev
至此,一个最纯净的vue3.0+vite2+typescript项目就完成了。在浏览地址栏中输入http://localhost:3000/,就看到了如下的启动页,然后就可以安装所需的插件了。
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path' // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], resolve: { alias: { '@': path.resolve(__dirname, 'src'), }, }, })
{ "compilerOptions": { "target": "esnext", "module": "esnext", "moduleResolution": "node", "strict": true, "jsx": "preserve", "sourceMap": true, "resolveJsonModule": true, "esModuleInterop": true, "lib": ["esnext", "dom"], "baseUrl": ".", "paths": { "@/*":["src/*"] } }, "include": [ "src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue" ] }
# npm
npm install vue-router@4
# yarn
yarn add vue-router@4
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router' const routes: RouteRecordRaw[] = [ { path: '/', name: 'Login', // 注意这里要带上文件后缀.vue component: () => import('@/pages/login/Login.vue'), meta: { title: '登录', }, }, ] const router = createRouter({ history: createWebHistory(), routes, strict: true, // 期望滚动到哪个的位置 scrollBehavior(to, from, savedPosition) { return new Promise(resolve => { if (savedPosition) { return savedPosition; } else { if (from.meta.saveSrollTop) { const top: number = document.documentElement.scrollTop || document.body.scrollTop; resolve({ left: 0, top }); } } }); } }) export function setupRouter(app: App) { app.use(router); } export default router
import {
createApp } from "vue";
import App from "./App.vue";
import router, {
setupRouter } from './router';
const app = createApp(App);
// 挂在路由
setupRouter(app);
// 路由准备就绪后挂载APP实例
await router.isReady();
app.mount('#app', true);
更多的路由配置可以移步vue-router(https://next.router.vuejs.org/zh/introduction.html)。 vue-router4.x支持typescript,路由的类型为RouteRecordRaw。meta字段可以让我们根据不同的业务需求扩展 RouteMeta 接口来输入它的多样性。以下的meta中的配置仅供参考:
// typings.d.ts or router.ts import 'vue-router' declare module 'vue-router' { interface RouteMeta { // 页面标题,通常必选。 title: string; // 菜单图标 icon?: string; // 配置菜单的权限 permission: string[]; // 是否开启页面缓存 keepAlive?: boolean; // 二级页面我们并不想在菜单中显示 hidden?: boolean; // 菜单排序 order?: number; // 嵌套外链 frameUrl?: string; } }
yarn add sass-loader --dev
yarn add dart-sass --dev
yarn add sass --dev
$white: #FFFFFF;
$primary-color: #1890ff;
$success-color: #67C23A;
$warning-color: #E6A23C;
$danger-color: #F56C6C;
$info-color: #909399;
css: {
preprocessorOptions: {
scss: {
modifyVars: {
},
javascriptEnabled: true,
// 注意这里的引入的书写
additionalData: '@import "@/style/varibles.scss";'
}
}
},
在组件中使用
.div {
color: $primary-color;
background-color: $success-color;
}
import Axios, {
AxiosRequestConfig, CancelTokenStatic, AxiosInstance } from "axios";
import NProgress from 'nprogress'
import {
genConfig } from "./config";
import {
transformConfigByMethod } from "./utils";
import {
cancelTokenType,
RequestMethods
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。