在Django+Vue3+GraphQL的Blog例子代码中引入Element-Plus UI Framework

yizhihongxing

Vue3的UI Framework中有Element-Plus、BalmUI、Quasar、PrimeVue、Ant Design Vue等UI Framework.

Element-Plus是Element-UI的Vue3版,Element-UI的使用人数的基数较大,Github上的Star数也较多,就选择了Element-Plus作为这个Blog项目的UI Framework.

UI Framework的好处就是提供了相较裸Html+CSS开发起来更好看和方便一些.

Element-Plus在国内有镜像站点,参看起来速度相对快一些. Element-Plus国内镜像站点.

代码可以从Github上取得https://github.com/magicduan/django_vue_graphql/releases/tag/base_0.2

配置Element-UI开发环境

如果在已有的项目中安装的用包管理器安装最方便了.

 npm install element-plus --save

由于对Element-Plus的组件的使用方法不是很熟练, 按照官网的说明我直接从Element-Plus提供的Vite的快速模版进行启动.

https://github.com/element-plus/element-plus-vite-starter

从Github上下载代码后展开,执行下列命令就可以启动一个Element-Plus的Demo服务了.

npm install
npm run dev

按照前一篇Blog例子代码, 修改vite.config.ts,加入端口配置,将Vue的端口设置为8080

export default defineConfig({
....
  server:{
    port:8080
  },
....

使用npm 安装vue-router、apollo-client

npm install vue-router@4
npm install --save graphql graphql-tag @apollo/client
npm install --save @vue/apollo-composable

 

迁移代码

由于是后安装的vue-router,缺省的vue-router 没有设置, 建立与Frontend相同的目录

src/router
src/views

将Blog项目中的Frontend中相关的代码Copy进入对应目录

src/router/index.ts
src/views/AllPosts.vue
src/views/AuthorView.vue
src/views/PostsByTag.vue
src/views/PostView.vue
src/componets/AuthorLink.vue
src/componets/PostLis.vue

修改顶部菜单BaseHeader.vue

Element-UI模版提供的是顶菜单+左工具栏的UI模版, src/compoments/layout/BaseHeader.vue 为顶部菜单, BaseSide.vue为左边菜单.

这里我们修改BaseHeader.vue,修改后菜单项为

MyBlog --> HomeView.vue
Posts 
   All Posts --> All Posts
   Add Post //将来实现
   Delete Post//将来实现
Tag ->根据Backend端的取得Tag的种类动态生成Tag菜单
HelloWord -->对应原Element-UI的Helloworld的例子

由于需要根据BackEnd端的Tag查询结果动态生成Tag,我们为BaseHeader.vue加入Props :tagItems

Baseheader.vue代码具体修改如下:

<script setup lang="ts" >
import { toggleDark } from '~/composables';
import { ref } from 'vue';
const props = defineProps({
  tagItems:{type:Array,required:true, default:[]}
})

const msg = ref("/hello/\"Hello Vue 3.0 + Element Plus + Vite\"")

</script>
<template>
  <el-menu class="el-menu-demo" mode="horizontal" :router= true>
    <el-menu-item index="/">My Blog</el-menu-item>
    <el-sub-menu index="2">
      <template #title>Posts</template>
      <el-menu-item index="/posts">All Posts</el-menu-item>
      <el-menu-item index="2-1">Add Post</el-menu-item>
      <el-menu-item index="2-2">Delete Post</el-menu-item>
      <el-sub-menu index="2-4">
        <template #title>...</template>
        <el-menu-item index="2-4-1">item one</el-menu-item>
        <el-menu-item index="2-4-2">item two</el-menu-item>
        <el-menu-item index="2-4-3">item three</el-menu-item>
      </el-sub-menu>
    </el-sub-menu>

    <el-sub-menu v-if="tagItems.length" index="tags" >
      <template #title>Tags</template>
      <el-menu-item v-for="tagInfo in tagItems" :index="tagInfo.path">
        {{ tagInfo.name }}
      </el-menu-item>
    </el-sub-menu>
    <el-menu-item v-else :index="tags" disabled>Tags</el-menu-item>

    <el-menu-item :index="msg" >HelloVue</el-menu-item>

    <el-menu-item h="full" @click="toggleDark()">
      <button class="border-none w-full bg-transparent cursor-pointer" style="height: var(--ep-menu-item-height);">
        <i inline-flex i="dark:ep-moon ep-sunny" />
      </button>
    </el-menu-item>
  </el-menu>
</template>

 其中需要注意的几点:

  • 将菜单项与vue-router结合起来,菜单项的index属性为router/index.ts中设置的URL path, 需要设置el-menu
    :router= true
  • HelloWorld.vue 其中需要属性msg,为了在菜单中进行配置,我们需要在index.ts中将HelloWord.vue的URL的prop设置为true
  
const router = createRouter({
...  
    {
      path: '/hello/:msg',
      name: 'HellowWorld',
      component: () => import("../components/HelloWorld.vue"),
      props: true  
    },
...

将HelloWorld.vue Component的Prop属性作为参数来传递

修改App.vue

在App.vue中加入从Backend通过GraphQL取Tags的代码, 将Tags数组传递给BaseHeader.vue,将<HelloWord .../>改为<routerview/>

<script setup lang="ts">
import HomeView from './views/HomeView.vue';
import HelloWorld from './components/HelloWorld.vue';
import gql from 'graphql-tag';
import { useQuery } from '@vue/apollo-composable';
import { computed } from '@vue/reactivity';

const {result,loading,error} =  useQuery(gql`
  query getTags{
    tags {
          name
    }
  }
`)

const tags = computed(()=>{
  let tagsArray = []
  if (result.value){
    result.value?.tags.forEach(tagName => {
      let tagInfo = {path:"/tag/"+tagName.name,name:tagName.name}
      tagsArray.push(tagInfo)
    });
  }
  return tagsArray

})

</script>
<template>
  <el-config-provider namespace="ep" >
    <BaseHeader :tagItems="tags"/>
    <div style="display: flex">
      <BaseSide />
      <div>
        <!-- <HelloWorld msg="Hello Vue 3.0 + Element Plus + Vite" @changeTags ='changTags'/>  -->
        <RouterView/> 
      </div>
    </div>
  </el-config-provider>
</template>

<style>
#app {
  text-align: center;
  color: var(--ep-text-color-primary);
}

.element-plus-logo {
  width: 50%;
}
</style>

在Backend中加入Tags的查询

backend/blog/scheme/py

class Query(graphene.ObjectType):
    ....
    tags = graphene.List(TagType)
    .....
    def resolve_tags(root,info):
        return (
            models.Tag.objects.all()
        )

 

修改main.ts加入router,Apollo相关的代码

import { DefaultApolloClient } from '@vue/apollo-composable'
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core'

// HTTP connection to the API
const httpLink = createHttpLink({
  uri: 'http://localhost:8000/graphql',
})

// Cache implementation
const cache = new InMemoryCache()

// Create the apollo client
const apolloClient = new ApolloClient({
  link: httpLink,
  cache,
})

const app = createApp({
    setup () {
      provide(DefaultApolloClient, apolloClient)
    },
  
    render: () => h(App),
  })

app.use(router)

 

至此blog相关的代码迁移完成, 可以完成Posts List等操作了,但是页面相对不太好看,下一步我们使用Element-UI的控件进行对应改造即可.

使用Element-UI改造Posts等相关的Vue

在技术打通之后,UI的改造就是细心+审美的工作了,按照我自己的意思进行了修改,具体的修改就不赘述了,直接参考代码即可.

 

原文链接:https://www.cnblogs.com/magicduan/p/17315028.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:在Django+Vue3+GraphQL的Blog例子代码中引入Element-Plus UI Framework - Python技术站

(0)
上一篇 2023年4月17日
下一篇 2023年4月17日

相关文章

  • Python Requests爬虫之求取关键词页面详解

    Python Requests爬虫之求取关键词页面详解 介绍 Python Requests库是一个常用的用于发送HTTP请求的库,可用于构建各种爬虫、自动化工具和Web应用。本攻略主要讲解如何使用Python Requests库进行关键词页面的爬取。 准备工作 在使用前我们需要先安装Python Requests库: pip install request…

    python 2023年5月14日
    00
  • Python的设计模式编程入门指南

    Python的设计模式编程入门指南 设计模式是一种被广泛应用于软件开发中的解决问题的方法。Python是一种非常流行的编程语言,它提供了许多工具和库来实现各种设计模式。在本文中,我们将介绍Python中的一些常见的设计模式,并提供示例说明。 什么是设计模式? 设计模式是一种被广泛应用于软件开发中的解决问题的方法。它是一种被证明有效的解决方案,可以帮助开人员解…

    python 2023年5月14日
    00
  • Python 第三方库 Pandas 数据分析教程

    Pandas是一个用于数据分析和处理的强大Python第三方库。本教程将介绍Pandas的使用方法,以便您可以开始使用Pandas进行数据分析和处理工作。下面是一个完整实例教程,包括两个示例。 选择适当的数据结构 在使用Pandas进行数据分析和处理之前,需要选择适当的数据结构。Pandas提供了两种主要的数据结构:Series和DataFrame。 Ser…

    python 2023年5月13日
    00
  • python将字符串转换成数组的方法

    让我详细的给您介绍一下Python转换字符串为数组的方法。 将字符串转换成数组是Python编程中非常重要的任务之一,因为它可以让您更好地处理和操作数据。Python提供了多种方法将字符串转换为数组。下面我们将介绍三种最常用的方法。 方法一:使用split函数将字符串拆分成单词列表 使用split函数是将字符串转换成数组的最简单和最常用的方法之一。所谓spl…

    python 2023年6月5日
    00
  • Python3 元组tuple入门基础

    Python3元组tuple入门基础 在Python中,元组(tuple)是一个有序且不可变的序列。这意味着一旦定义,元组中的元素就不可以更改。 创建元组 元组的创建方式相对简单,只需要用小括号将元素括起来即可。例如: mytuple = (1, 2, 3) print(mytuple) # 输出 (1, 2, 3) 需要注意,在定义只有一个元素的元组时,必…

    python 2023年5月14日
    00
  • python中使用docx模块处理word文档

    下面我将详细讲解如何在Python中使用docx模块处理Word文档。整个过程包含以下几个步骤: 安装docx模块 使用pip命令安装docx模块,可以使用以下命令: pip install python-docx 打开Word文档 使用docx模块中的Document类打开Word文档,可以使用以下代码: from docx import Document…

    python 2023年6月3日
    00
  • Python使用py2neo操作图数据库neo4j的方法详解

    Python使用py2neo操作图数据库neo4j的方法详解 什么是neo4j Neo4j 是一个高度可扩展的、本质上是 ACID 的、即时图形数据库, 使用原始的负载贝尔格共享架构。 Neo4j 被优化为大量复杂的图操作和高并发性 安装neo4j 在官网下载neo4j服务器 遵照提示安装neo4j服务器 Python与neo4j的连接 Python官方提供…

    python 2023年5月14日
    00
  • Python如何获得百度统计API的数据并发送邮件示例代码

    Python如何获得百度统计API的数据并发送邮件示例代码 百度统计是一款网站分析工具,可以帮助网站管理员了解网站的访问情况、用户行为等信息。百度统计提供了API接口,可以通过API接口获取网站的访问数据。以下是两个示例,介绍了如何使用Python获得百度统计API的数据并发送邮件。 示例一:使用Python获得百度统计API的数据 以下是一个示例,可以使用…

    python 2023年5月15日
    00
合作推广
合作推广
分享本页
返回顶部