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>
相关推荐
王哈哈^_^1 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
cs_dn_Jie2 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic2 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿2 小时前
webWorker基本用法
前端·javascript·vue.js
cy玩具3 小时前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
customer083 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
清灵xmf3 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据3 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161774 小时前
防抖函数--应用场景及示例
前端·javascript
334554324 小时前
element动态表头合并表格
开发语言·javascript·ecmascript