vue3.0 amis 低代码框架

文章介绍了如何在Vue3项目中使用Amis,一个由百度开源的低代码框架,通过JSON配置快速生成页面。文章详细展示了安装步骤、封装组件AmisComponent以及动态渲染组件LowcodeEngine的实现过程,同时提到了axios的使用和接口请求的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

amis

amis 是百度开源的一个低代码前端框架,它使用 JSON 配置来生成页面,可以减少页面开发工作量,极大提升效率。

组件地址:https://aisuda.bce.baidu.com/amis

编辑器地址:https://aisuda.github.io/amis-editor

Vue3 中使用

安装依赖

npm i amis

npm i copy-to-clipboard

封装组件 AmisComponent

<template> 
    <div id="amisid" ref="boxRef"></div>
</template>
<script setup lang="ts">
    import {defineProps,watch,ref} from "vue"
    import {ElMessage} from 'element-plus'
    import 'amis/sdk/sdk.js'
    import 'amis/lib/themes/default.css'
    import axios from 'axios'
    import copy from 'copy-to-clipboard'

    const props = defineProps({
        formid: {
            type: String,
            default: () => {
                return ''
            }
        },
        formjson: {
            type: Object,
            default: () => {
                return {}
            }
        }
    })

    // @ts-ignore
    const amis = amisRequire('amis/embed');
    const boxRef = ref(null)

    watch(()=> props.formjson, (data)=>{
            onRendering(data)
        },
        {immediate: true,deep: true}
    )

    function onRendering(data:any){
        let theme = 'cxd'
        let amisScoped = amis.embed( 
            '#amisid',
            data,
            {
                updateLocation: (to, replace) => {},
            },
            {
                // 下面三个接口必须实现
                fetcher: ({
                    url, // 接口地址
                    method, // 请求方法 get、post、put、delete
                    data, // 请求数据
                    responseType,
                    config, // 其他配置
                    headers ,// 请求头
                    updateLocation
                }) => {
                    config = config || {};
                    config.withCredentials = true;
                    
                    // 设置接口地址
                    config.baseURL = import.meta.env.VITE_APP_BASE_API;
            
                    responseType && (config.responseType = responseType);
            
                    if (config.cancelExecutor) {
                        config.cancelToken = new (axios).CancelToken(
                            config.cancelExecutor
                        );
                    }
            
                    config.headers = headers || {};
                    
                    // 设置token
                    const isToken = (config.headers || {}).isToken === false
                    if (!isToken) {
                        config.headers['token'] = 'xxxx-xxxx-xxxx-xxxx' // 自行实现逻辑
                    }
            
                    if (method !== 'post' && method !== 'put' && method !== 'patch') {
                        if (data) {
                            config.params = data;
                        }
                
                        return (axios )[method](url, config);
                    } else if (data && data instanceof FormData) {
                        config.headers = config.headers || {};
                        config.headers['Content-Type'] = 'multipart/form-data';
                    } else if (data && typeof data !== 'string' && !(data instanceof Blob) && !(data instanceof ArrayBuffer)) {
                        data = JSON.stringify(data);
                        config.headers = config.headers || {};
                        config.headers['Content-Type'] = 'application/json';
                    }
            
                    return (axios )[method](url, data, config);
                },
                isCancel: (value) => (axios ).isCancel(value),
                copy: content => {
                    copy(content);
                    ElMessage.success('内容已复制到粘贴板');
                },
                theme
            }
        )
    }
</script>

动态渲染组件 LowcodeEngine

<template> 
    <amisComponent :formid="formid" :formjson="formjson"></amisComponent>
</template>
<script setup lang="ts">
    import {reactive, watch, ref} from "vue"
    import amisComponent from "../amis/AmisComponent.vue"
    import {useRoute,useRouter} from 'vue-router'

    const route = useRoute()
    const router = useRouter()
    const formid = ref('')
    const formjson = ref({})

    watch(()=> router.currentRoute, (data)=>{
            let _router:any = data.value
            formid.value = res.result.id
            formjson.value = {"type": "page","title": "标题","body": "Hello World!"}
            // TODO 根据绑定菜单获取页面JSON
        },
        {immediate: true,deep: true}
    )
    
</script>

路由处理

import request from '@/utils/request'
import Layout from '@/components/layout/Index.vue'

const modules = import.meta.glob('../views/**/**.vue')
const layoutModules = import.meta.glob("../components/layout/**.vue")

// 根据菜单构建动态路由
export const filterRouter = (routers:any, level:any) => {
    level = level || 0
    const accessedRouters = routers.filter( (router:any) => {
        if (router.isShow) {
            router.component = layoutModules[`../components/layout/LowcodeEngine.vue`]

            if (router.children && router.children.length) {
                router.children = filterRouter(router.children, level + 1)
            }
            return true
        }
        return false
    })
    return accessedRouters
}

项目地址

Gitee: https://gitee.com/typ1805/tansci-boot

GitHub: https://github.com/typ1805/tansci-boot

<think>嗯,用户想在Vue3项目中使用Amis框架,需要示例代码或教程。首先,我得确认Amis是否支持Vue3。根据之前的引用,用户提供的参考中有提到Vue的示例代码,但那是Vue2的,比如引用[1]里的代码用的是`export default`和`data()`函数,这是Vue2的写法。不过Amis官方可能有更新支持Vue3的情况,需要确认。 接下来,我应该检查Amis的官方文档是否有Vue3的相关说明。假设官方已经支持,那么需要给出安装步骤和示例代码。通常,在Vue3中使用第三方库,可能需要使用`vue-amis`的npm包,或者直接通过CDN引入。然后,在组件中注册并使用Amis的组件。 另外,用户提到示例代码和教程,可能需要分步骤说明,比如创建Vue3项目、安装依赖、配置Amis、编写示例组件等。需要确保代码的语法正确,比如使用Composition API或者Options API,但Vue3更推荐Composition API,不过为了兼容,可能两者都可以。 然后,考虑到用户可能需要更多的资源,应该提供官方文档链接,以及可能的GitHub示例或社区教程。同时,用户可能遇到的问题,比如配置不生效、样式问题等,可以给出一些常见解决办法,比如检查引入路径、版本兼容性等。 最后,生成相关问题,比如如何集成Amis表单,如何自定义组件,性能优化等,帮助用户进一步探索。</think>### Vue3集成Amis框架的步骤及示例 在Vue3项目中使用Amis框架需要以下步骤,这里提供两种实现方式: **方法一:通过CDN直接引入** ```html <template> <div id="amis-root"></div> </template> <script setup> import { onMounted } from &#39;vue&#39;; onMounted(() => { const amis = window.amisRequire(&#39;amis/embed&#39;); const amisJSON = { type: &#39;page&#39;, body: { type: &#39;form&#39;, controls: [ { type: &#39;text&#39;, name: &#39;username&#39;, label: &#39;用户名&#39; } ] } }; amis.embed(&#39;#amis-root&#39;, amisJSON); }); </script> ``` **方法二:通过npm包安装** 1. 安装依赖: ```bash npm install amis @fex/amis-embed-vue3 ``` 2. 创建Amis组件: ```vue <template> <AmisRenderer :schema="schema" /> </template> <script setup> import { AmisRenderer } from &#39;@fex/amis-embed-vue3&#39;; const schema = { type: &#39;page&#39;, body: { type: &#39;crud&#39;, api: &#39;/api/mock/data&#39;, columns: [ { name: &#39;id&#39;, label: &#39;ID&#39; }, { name: &#39;name&#39;, label: &#39;名称&#39; } ] } }; </script> ``` **重要配置说明**: 1. 需要配置Vue的全局组件(在main.js中): ```javascript import { createApp } from &#39;vue&#39; import App from &#39;./App.vue&#39; import { install as AmisEmbed } from &#39;@fex/amis-embed-vue3&#39; const app = createApp(App) app.use(AmisEmbed) app.mount(&#39;#app&#39;) ``` **官方推荐资源**: - Amis官方文档中Vue集成章节[^2] - Amis示例库中的Vue3集成分支(GitHub) - 社区实现的vue3-amis-template模板项目
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值