mirror of
https://gitee.com/ssssssss-team/magic-boot.git
synced 2026-06-02 00:00:03 +08:00
全局注册 动态组件
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { babelParse } from '@vue/compiler-sfc'
|
||||
import { compileFile } from '@/compiler/sfc-compiler.js'
|
||||
|
||||
function appComponent(app, item){
|
||||
var compiled = {}
|
||||
compileFile('TestCode.vue', item.code, compiled)
|
||||
var code = compiled.js
|
||||
var ast = babelParse(code, {
|
||||
sourceType: 'module'
|
||||
})
|
||||
var replaceCode = (node, subCode) => code.substring(0, node.start) + subCode + code.substring(node.end);
|
||||
for(var i = ast.program.body.length - 1; i>=0; i--){
|
||||
var node = ast.program.body[i]
|
||||
if(node.type === 'ImportDeclaration'){
|
||||
code = replaceCode(node, node.specifiers.map(it => `const ${it.local?.name || it.imported?.name || '*'} = ___magic__import__('${node.source.value}', '${it.imported?.name || '*'}');`).join('\r\n'));
|
||||
} else if (node.type === 'ExportDefaultDeclaration'){
|
||||
code = replaceCode(node, `return ${node.declaration.name}`)
|
||||
}
|
||||
}
|
||||
code = `(function(){
|
||||
${code}
|
||||
})()`
|
||||
var componentStyle = document.createElement("style");
|
||||
componentStyle.innerHTML = compiled.css
|
||||
document.head.appendChild(componentStyle);
|
||||
app.component(item.name, eval(code))
|
||||
}
|
||||
|
||||
const install = (app) => {
|
||||
app.config.globalProperties.$get('/component/list', { size: 999999 }).then((res) => {
|
||||
res.data.list.forEach(it => {
|
||||
appComponent(app, it)
|
||||
})
|
||||
})
|
||||
}
|
||||
export default install
|
||||
@@ -27,4 +27,4 @@ const install = (app) => {
|
||||
}
|
||||
app.use(VueUeditorWrap)
|
||||
}
|
||||
export default install
|
||||
export default install
|
||||
|
||||
@@ -7,11 +7,8 @@ export const filterAsyncRouter = (routers, level) => {
|
||||
const accessedRouters = routers.filter(router => {
|
||||
if (router.isShow === 1) {
|
||||
if (router.componentName) {
|
||||
router.component = loadView(`/common/parse-component`)
|
||||
router.props = {
|
||||
name: router.componentName,
|
||||
code: router.code
|
||||
}
|
||||
router.component = loadView(`/common/show-component`)
|
||||
router.props = { name: router.componentName }
|
||||
} else if (router.component) {
|
||||
const component = router.component
|
||||
if (component === 'Layout') {
|
||||
@@ -52,4 +49,4 @@ export function generateRoutes(){
|
||||
resolve(asyncRouter)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user