木鸟 发表于 2023-4-13 18:05:48

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

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代码具体修改如下:
<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({
...
    {
      <strong>path: </strong><strong>'/hello/:msg'</strong>,
      name: 'HellowWorld',
      component: () => import("../components/HelloWorld.vue"),
   <strong> props: </strong><strong>true </strong>
    },
...将HelloWorld.vue Component的Prop属性作为参数来传递
修改App.vue

在App.vue中加入从Backend通过GraphQL取Tags的代码, 将Tags数组传递给BaseHeader.vue,将改为
<template>
<el-config-provider namespace="ep" >
    <BaseHeader :tagItems="tags"/>
   
      <BaseSide />
      
      
      <RouterView/>
      
   
</el-config-provider>
</template>在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
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 在Django+Vue3+GraphQL的Blog例子代码中引入Element-Plus UI Framework