• 基于 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
    
    • 1

    yarn

    yarn create vite
    
    • 1

    创建项目

    npm

    npm create vite@latest my-vue-app --template vue-ts
    
    • 1
    // 进入项目目录
    cd my-vue-app
    // 安装插件
    npm install
    // 启动
    npm run dev
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    yarn

    yarn create vite my-vue-app --template vue-ts
    
    • 1
    // 进入项目目录
    cd my-vue-app
    // 安装插件
    yarn
    // 启动
    yarn dev
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    启动

    在这里插入图片描述

    sass

    安装

    npm

    npm install sass sass-loader -D
    
    • 1

    yarn

    yarn add sass sass-loader -D
    
    • 1

    使用

    注意: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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    效果

    在这里插入图片描述

    router

    安装

    npm

    npm install vue-router@4
    
    • 1

    yarn

    yarn add vue-router@4
    
    • 1

    配置

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

    src 目录下

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

    home.vue

    <template>
        <div>
            <h1>Home</h1>
        </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    about.vue

    <template>
        <div>
            <h1>About</h1>
        </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    main.ts 中挂载根实例

    import { createApp } from 'vue'
    import App from './App.vue'
    import router from 'router/index.ts'
    
    createApp(App)
    .use(router) // 挂载到根实例
    .mount('#app')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    改造 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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    效果

    在这里插入图片描述

    element-plus

    安装

    官网在此

    npm

    npm install element-plus --save
    
    • 1

    yarn

    yarn add element-plus
    
    • 1

    引入

    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')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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

    {
      "compilerOptions": {
        // ...
        "types": ["element-plus/global"]
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用

    src/views/home.vue

    <template>
        <div>
            <h1>Home</h1>
            <el-button type="primary">测试按钮</el-button>
        </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    效果

    在这里插入图片描述

  • 相关阅读:
    [computer vision] Bag of Visual Word (BOW)
    在两个排序数组中找到上中位数
    科技资讯|苹果Vision Pro可通过手势ID检测不同用户
    IOS逆向初探
    数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
    php毕业设计美食菜谱网站
    stm32f103最小系统板详细介绍
    第6篇 vue的打包工具webpack
    SpringBoot异步任务获取HttpServletRequest
    嬴彻科技日: 发布《自动驾驶卡车量产白皮书》分享从量产走向无人技术路线
  • 原文地址:https://blog.csdn.net/sinat_31213021/article/details/125372222