Vue+Vite项目初建(axios+Unocss+iconify)

一. 创建项目

npx --package @vue/cli vue

项目成功启动后,进入http://localhost:3200,即可进入创建好的页面(假设启动端口为3200)

二. 测试网络通讯模块

假设有本地服务器地址localhost:8000提供接口服务,接口为localhost:8000/token,修改代码

html 复制代码
<script setup>

import {ref} from 'vue'
import axios from 'axios'
import qs from 'qs'
import 'unocss'

defineProps({
  msg: String,
})

function clickTest() {
  console.log("按钮点击")

  // var axios = require('axios');
  var data = qs.stringify({
    'username': 'hahaha',
    'password': '123456'
  });
  var config = {
    method: 'post',
    url: 'http://localhost:8000/token',
    headers: {
      'Access-Control-Allow-Credentials': 'True',
      'Access-Control-Allow-Origin': '*/*'
    },
    data: data
  };

  axios(config)
      .then(function (response) {
        console.log(JSON.stringify(response.data));
      })
      .catch(function (error) {
        console.log(error);
      });
}

const count = ref(0)
</script>

<template>
  <h1>{{ msg }}</h1>
  <button v-on:click="clickTest">测试</button>
  <p>
    Check out
    <a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
    >create-vue</a
    >, the official Vue + Vite starter
  </p>
  <p>
    Install
    <a href="https://github.com/vuejs/language-tools" target="_blank">Volar</a>
    in your IDE for a better DX
  </p>
  <p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>

执行代码,如果后端服务器执行正常,就会有相应的返回值

三. 集成、测试UnoCSS

1. 安装

npm install -D unocss

2. 集成

修改 vite.config.js

html 复制代码
import UnoCSS from 'unocss/vite'

新建 uno.config.js

html 复制代码
import {defineConfig, presetUno, presetIcons, presetAttributify} from 'unocss'
import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders'

export default defineConfig({
    presets: [
        presetUno(), // 添加 UnoCSS 的默认样式预设
        presetAttributify(),
        presetIcons({
            warn: true,
            prefix: ['i-'],
            extraProperties: {
                display: 'inline-block',
            },
            collections: {
                me: FileSystemIconLoader('./src/assets/isme'),
                fe: FileSystemIconLoader('./src/assets/feather'),
            },
        })
    ],
})

以上代码用于配置unocss到系统中,注意"collections"中的配置是自定义图标的路径

  1. 显示
html 复制代码
  <div class="text-center text-red-500">
    Hello World!
  </div>

显示出以上效果说明unocss配置成功。

  1. 图标()
html 复制代码
  <div m2 f-c hover="op80">
    <a i-carbon-logo-github text-3xl text-black href="https://github.com/chris-zhu" target="_blank" />
    <div i-carbon:3d-cursor text-3xl text-blue />
    <button text-green text-3xl class="i-carbon-sun" />
    <i class="i-me:gitee mr-12 cursor-pointer"/> // 自定义图标
  </div>
  <div class="i-carbon:content-view w-1em h-1em"></div>
  <div class="i-carbon:humidity w-1em h-1em"></div>

  <div class="card">
    <button type="button" @click="count++">count is {{ count }}</button>
    <button v-on:click="clickTest">测试</button>
    <p>
      Edit
      <code>components/HelloWorld.vue</code> to test HMR
    </p>
  </div>

最终显示效果,基本完成前期开发配置需要

相关推荐
码云之上1 分钟前
从一个截图函数到一个 npm 包——pdf-snapshot 的诞生记
前端·node.js·github
码事漫谈31 分钟前
AI提效,到底能强到什么程度?
前端·后端
IT_陈寒31 分钟前
React hooks依赖数组这个坑差点把我埋了
前端·人工智能·后端
阿祖zu1 小时前
内容创作 AI 透明化声明倡议与项目开源
前端·后端·github
lpfasd1231 小时前
TypeScript + Cloudflare 全家桶部署项目全流程
前端·javascript·typescript
ZC跨境爬虫1 小时前
极验滑动验证码自动化实战:背景提取、缺口定位与Playwright滑动模拟
前端·爬虫·python·自动化
前端Hardy1 小时前
字节/腾讯内部流出!Claude Code 2026王炸玩法!效率暴涨10倍
前端·javascript·vue.js
糟糕好吃1 小时前
AI 全流程解析(LLM / Token / Context / RAG / Prompt / Tool / Skill / Agent)
前端·后端·设计模式
快手技术2 小时前
快手广告系统全面迈入生成式推荐时代!GR4AD:从Token到Revenue的全链路重构
前端·后端
前端Hardy2 小时前
大厂都在偷偷用的 Cursor Rules 封装!告别重复 Prompt,AI 编程效率翻倍
前端·javascript·面试