iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >一文教会你搭建vite项目并配置路由和element-plus
  • 244
分享到

一文教会你搭建vite项目并配置路由和element-plus

2024-04-02 19:04:59 244人浏览 泡泡鱼
摘要

目录1.创建项目2.安装vite3.启动项目4.可在vite.config.ts文件下修改端口号,默认为3030,我们可以改成习惯用的80805.安装路由router和element

1.创建项目

npm init vite@latest m-component -- --template vue-ts

2.安装vite

npm i

3.启动项目

npm run dev

4.可在vite.config.ts文件下修改端口号,默认为3030,我们可以改成习惯用的8080

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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server:{
    port:8080
  }
})

5.安装路由router和element-plus

npm i -S vue-router@next element-plus

可在package.JSON中查看下载的路由和element-plus配置信息

6.在src目录下新建views和router文件夹,然后在router目录下新建index.ts文件,在index.ts文件下配置路由

import { createRouter,createWEBHistory,RouteRecordRaw } from "vue-router";
import Home from "../views/Home.vue"
const routes:RouteRecordRaw[] = [
    {
        path:'/',
        component:Home
    }
]
const router = createRouter({
    routes,
    history:createWebHistory()
})

export default router

在views目录下新建一个Home.vue文件

<template>
    <div>
       首页
    </div>
</template>

<script lang="ts" setup>

</script>

<style lang="sCSS" scoped>
</style>

6.然后在main.ts中引入

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

const app =createApp(App)
app.use(router)
app.mount('#app')

7.使用element-plus

在main.ts中引入使用

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

const app =createApp(App)
app.use(router).use(ElementPlus)
app.mount('#app')

在Home.vue中

<template>
    <div>
        <el-button>按钮</el-button>
    </div>
</template>

<script lang="ts" setup>

</script>

<style lang="scss" scoped>
</style>

8.在App.vue中编写样式

<template>
    <router-view></router-view>
</template>

<style>
*{
    margin: 0;
    padding: 0;
}
</style>

这里使用的scss,需要先安装sass 和sass-loader(*这是css的预处理器)

npm i -D sass sass-loader

附:vite引入element-plus修改主题色报错

原因:引入文件路径不对

解决:~改成node_modules/,安装scss --dev,然后引入时去掉.scss/.css,完美运行

$--color-primary: #62c28c;
 

$--font-path: "node_modules/element-plus/lib/theme-chalk/fonts";
@import "node_modules/element-plus/packages/theme-chalk/src/index";

总结

到此这篇关于搭建vite项目并配置路由和element-plus的文章就介绍到这了,更多相关vite搭建并配置路由element-plus内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 一文教会你搭建vite项目并配置路由和element-plus

本文链接: https://www.lsjlt.com/news/164590.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作