请教一下写前端的各位大佬, vue 动态组件如何动态定义名字呢?

2024-07-23 15:12:46 +08:00
 Jinnrry

vue 版本:3.3

背景:我项目里面希望给第三方提供一个页面。第三方通过网络接口的方式返回 html 代码,我程序里面把别人的 html 代码嵌入到我的页面中。

目前想到的方案:

1 、使用 v-html 标签嵌入。问题:这种方式嵌入,对方页面中如何调用我 vue 页面的方法属性呢?比如我这里有一个$http 变量是 axios 的实例,这个里面封装的验签相关处理,他必须用我这个$http 属性才能正常调用接口,不然他过不去验签。

2 、使用 vue 的异步组件。目前还没研究明白怎么用

下面是 demo 代码

<template>
    <div id="main">
        <el-tabs type="border-card">
        <el-tab-pane v-for="(html,name) in pluginList"  :label="name">
            // 方案 1
            <div v-html="html">

            </div> 
            
            // 方案 2
            
            <AsyncComp /> // 这样写的话第二个 plugin 又叫啥名字呢?
            
        </el-tab-pane>
    </el-tabs>
    </div>
</template>

<script setup>
import { reactive, ref, getCurrentInstance } from 'vue'
const app = getCurrentInstance()
const $http = app.appContext.config.globalProperties.$http
import { defineAsyncComponent } from 'vue'


const pluginList = reactive({})

$http.get('/api/plugin/list').then(res => {
    if (res.data != null && res.data.length > 0  ) {
        pluginList[res.data] = ""
        getPlugHtml(res.data)
    }
})



const getPlugHtml = function(name){

	 // 方案 1
	 $http.post('/api/plugin/settings/'+name+"/index").then(res => {
                if (res.data != null && res.data!=""  ) {
                    pluginList[name] = res.data
                }else{
                    pluginList[name] = "Load Error!"
                }
            })



	// 方案 2. 但是 AsyncComp 这个名字怎么处理呢?这里换成变量以后,我模板里面的代码该怎么调用异步组件呢?
    const AsyncComp = defineAsyncComponent(() => {
        return new Promise((resolve, reject) => {
            $http.post('/api/plugin/settings/'+name+"/index").then(res => {
                if (res.data != null && res.data!=""  ) {
                    resolve(res.data)
                }else{
                    reject("Plugin Load Error!")
                }
            })
            
        })
    })



}


</script>

3235 次点击
所在节点    Vue.js
45 条回复
Jinnrry
2024-07-24 11:22:11 +08:00
@ipwx #40 在 vue 项目里面找到一个 issues ,https://github.com/vuejs/core/discussions/6939

准备按这个思路搞,拿到字符串,调用 vue 的相关编译方法,动态编译生成组件
DesnLee
2024-07-24 16:07:28 +08:00
@Jinnrry #27 你这个为什么 template 包着 setup ?
Jinnrry
2024-07-24 16:11:45 +08:00
@bladey #38 比如他需要请求我系统的用户信息接口,拿到用户相关资料。然后拿用户资料跟他系统的 id 关联,接着处理他直接的逻辑

<template>
<div class="hello">
<MyComponent ></MyComponent>
</div>
</template>

<script setup>
import { defineAsyncComponent } from 'vue'

// 创建一个 Blob ,用于生成组件的 URL
const blob = new Blob([`<template> <div> hello world! </div> </template>`], { type: 'text/plain' });
const url = URL.createObjectURL(blob);

const MyComponent = defineAsyncComponent(() => {
return import(url);
});

</script>

安装你的思路写了要给 demo ,这个会报错 Cannot find module 'blob:http://localhost:8080/f795bf43-d4b9-4332-ac54-0139c19ce4e1'
at http://localhost:8080/js/app.js:191:11
Jinnrry
2024-07-24 16:12:32 +08:00
@DesnLee #42 回复的时候手敲的,敲错了
unco020511
2024-07-24 18:00:46 +08:00
你把所有需要桥接的对象和方法都挂载到 window 上不行吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1059455

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX