vue3+antd+g2plot快速入门

创建项目

bash 复制代码
pnpm create vite

选择vue和JavaScript

安装依赖

bash 复制代码
pnpm i ant-design-vue
pnpm i @antv/g2plot
pnpm i vue-router

完整代码

package.json

json 复制代码
{
  "name": "frontend",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@antv/g2plot": "^2.4.31",
    "ant-design-vue": "^4.2.3",
    "vue": "^3.4.29",
    "vue-router": "^4.4.0"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^5.0.5",
    "vite": "^5.3.1"
  }
}

src/main.js

js 复制代码
import {createApp} from 'vue'

import Antd from 'ant-design-vue';
import router from './router';

import 'ant-design-vue/dist/reset.css';

import App from './App.vue'

const app = createApp(App)

app.use(Antd)
app.use(router)

app.mount('#app')

src/App.vue

html 复制代码
<template>
  <RouterView/>
</template>

src/router/index.js

js 复制代码
import {createWebHashHistory, createRouter} from 'vue-router'

import Layout from '../page/layout/index.vue'
import Home from '../page/home/index.vue'
import LineQuickStart from '../page/line/quick_start.vue'

const routes = [
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        children: [
            {path: "/home", component: Home},
            {path: "/line/quickstart", component: LineQuickStart},
        ]
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router

src/page/layout/index.vue

html 复制代码
<script setup>
import {ref} from 'vue';
import {useRouter} from "vue-router";

const router = useRouter()
const collapsed = ref(false);
const selectedKeys = ref(['1']);


const handleLeftMenuClick = ({item, key, keyPath}) => {
  console.log(item, key, keyPath)
  router.push(key)
}

</script>

<template>
  <a-layout style="min-height: 100vh">
    <a-layout-sider v-model:collapsed="collapsed" collapsible>
      <a-menu
          v-model:selectedKeys="selectedKeys"
          theme="dark"
          mode="inline"
          @click="handleLeftMenuClick"
      >
        <a-menu-item key="home">
          <span>首页</span>
        </a-menu-item>
        <a-sub-menu key="line">
          <template #title>
            <span>
              <span>折线图</span>
            </span>
          </template>
          <a-menu-item key="/line/quickstart">快速入门</a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-content style="margin: 10px 16px">
        <div :style="{ padding: '24px', background: '#fff', minHeight: '460px' }">
          <RouterView/>
        </div>
      </a-layout-content>
      <a-layout-footer style="text-align: center">
        版权所有 © 2024 Python私教 张大鹏
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<style scoped>
#components-layout-demo-side .logo {
  height: 32px;
  margin: 16px;
  background: rgba(255, 255, 255, 0.3);
}

.site-layout .site-layout-background {
  background: #fff;
}

[data-theme='dark'] .site-layout .site-layout-background {
  background: #141414;
}
</style>

src/home/index.vue

html 复制代码
<script setup>

</script>

<template>
home
</template>

<style scoped>

</style>

src/line/quick_start.vue

html 复制代码
<script setup>
import {onMounted} from "vue";
import {Line} from "@antv/g2plot";

onMounted(() => {
  const data = [
    {year: '1991', value: 3},
    {year: '1992', value: 4},
    {year: '1993', value: 3.5},
    {year: '1994', value: 5},
    {year: '1995', value: 4.9},
    {year: '1996', value: 6},
    {year: '1997', value: 7},
    {year: '1998', value: 9},
    {year: '1999', value: 13},
  ];
  const line = new Line('container', {
    data,
    xField: 'year',
    yField: 'value',
  });
  line.render();
})
</script>

<template>
  <div id="container"></div>
</template>

<style scoped>

</style>
相关推荐
橙子家5 小时前
浏览器缓存之【身份与会话管理】:Cookies 和 Private state tokens
前端
To_OC6 小时前
LC 49 字母异位词分组:想到哈希表很简单,选对 key 才是精髓
javascript·算法·leetcode
最新资讯动态6 小时前
HDC 2026 | 对话鲸鸿动能:存量时代,品牌如何夺回营销“主动权”?
前端
最新资讯动态6 小时前
游戏出海,从产品走向体系
前端
最新资讯动态6 小时前
20人团队跑出百万DAU、大厂也来抢量:谁在鸿蒙生态跑出加速度
前端
最新资讯动态6 小时前
千万开发者背后,鸿蒙商业化的B面
前端
爱勇宝8 小时前
AI 时代:智商决定起点,情商决定走多远
前端·ai编程
kyriewen8 小时前
用了半年 Claude Code 后,我尝试关掉它写了一周代码——结果比想象中严重
前端·javascript·ai编程
IT_陈寒9 小时前
Vite的静态资源打包让我熬夜到三点,这坑千万别跳
前端·人工智能·后端
山河木马10 小时前
矩阵专题0-webGL中的矩阵
javascript·webgl·计算机图形学