Skip to content

Commit 909457a

Browse files
authored
fix(vue-generator): 优化区块引用路径,解决多层级路由问题 #1414 (#1415)
* fix(vue-generator): 优化区块引用路径,解决多层级路由问题 将 vue-generator 包中区块的相对路径从 '../components/' 统一修改为 '@/components/', 解决了多层级路由场景下区块引用路径不一致的问题。这个改动确保了在不同层级的页面中, 区块组件都能被正确引用和加载。 * test(vue-generator): Add test cases to verify the use of block-relative paths 添加新的测试用例 `case07`,用于验证在 Vue SFC 生成器中区块的相对路径是否正确解析为 `@/components/`。测试用例包括组件映射、页面 schema 和预期输出文件。
1 parent bff830a commit 909457a

7 files changed

Lines changed: 99 additions & 4 deletions

File tree

packages/vue-generator/src/generator/page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ const generateImports = (description, moduleName, type, componentsMap) => {
329329
blocks.push(`import ${block} from '${depPath}/${block}.vue'`)
330330
} else {
331331
const blockDefaultPath =
332-
type === 'Block' ? `import ${block} from './${block}.vue'` : `import ${block} from '../components/${block}.vue'`
332+
type === 'Block' ? `import ${block} from './${block}.vue'` : `import ${block} from '@/components/${block}.vue'`
333333

334334
blocks.push(blockDefaultPath)
335335
}

packages/vue-generator/src/generator/vue/sfc/genSetupSFC.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242

4343
const parseConfig = (config = {}) => {
4444
const {
45-
blockRelativePath = '../components/',
45+
blockRelativePath = '@/components/',
4646
blockSuffix = '.vue',
4747
scriptConfig = {},
4848
styleConfig = {}

packages/vue-generator/src/generator/vue/sfc/parseImport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const getImportMap = (schema, componentsMap, config) => {
4242
pkgMap[key].push(item)
4343
})
4444

45-
const { blockRelativePath = '../components', blockSuffix = '.vue' } = config
45+
const { blockRelativePath = '@/components', blockSuffix = '.vue' } = config
4646
const blockPkgMap = {}
4747
const relativePath = blockRelativePath.endsWith('/') ? blockRelativePath.slice(0, -1) : blockRelativePath
4848

@@ -67,7 +67,7 @@ export const getImportMap = (schema, componentsMap, config) => {
6767
export const genCompImport = (schema, componentsMap, config = {}) => {
6868
const { components, blocks } = parseImport(schema.children)
6969
const pkgMap = {}
70-
const { blockRelativePath = '../components/', blockSuffix = '.vue' } = config
70+
const { blockRelativePath = '@/components/', blockSuffix = '.vue' } = config
7171

7272
const importComps = componentsMap.filter(({ componentName }) => components.includes(componentName))
7373

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test } from 'vitest'
2+
import { genSFCWithDefaultPlugin } from '@/generator/vue/sfc'
3+
import schema from './page.schema.json'
4+
import componentsMap from './components-map.json'
5+
import { formatCode } from '@/utils/formatCode'
6+
7+
test('should use @/components/ as block relative path', async () => {
8+
const res = genSFCWithDefaultPlugin(schema, componentsMap)
9+
const formattedCode = formatCode(res, 'vue')
10+
11+
await expect(formattedCode).toMatchFileSnapshot('./expected/BlockPath.vue')
12+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"componentName": "BlockTest1a",
4+
"main": "./components"
5+
}
6+
]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<template>
2+
<div class="page-base-style">
3+
<span style="display: inline-block" class="component-base-style">测试二级页面使用区块</span>
4+
<block-test1a class="block-base-style"></block-test1a>
5+
</div>
6+
</template>
7+
8+
<script setup>
9+
import * as vue from 'vue'
10+
import { defineProps, defineEmits } from 'vue'
11+
import { I18nInjectionKey } from 'vue-i18n'
12+
import BlockTest1a from '@/components/BlockTest1a.vue'
13+
14+
const props = defineProps({})
15+
16+
const emit = defineEmits([])
17+
const { t, lowcodeWrap, stores } = vue.inject(I18nInjectionKey).lowcode()
18+
const wrap = lowcodeWrap(props, { emit })
19+
wrap({ stores })
20+
21+
const state = vue.reactive({})
22+
wrap({ state })
23+
</script>
24+
<style scoped>
25+
.page-base-style {
26+
padding: 24px;
27+
background: #ffffff;
28+
}
29+
30+
.block-base-style {
31+
margin: 16px;
32+
}
33+
34+
.component-base-style {
35+
margin: 8px;
36+
}
37+
</style>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"componentName": "Page",
3+
"css": ".page-base-style {\n padding: 24px;background: #FFFFFF;\n}\n\n.block-base-style {\n margin: 16px;\n}\n\n.component-base-style {\n margin: 8px;\n}\n",
4+
"props": {
5+
"className": "page-base-style"
6+
},
7+
"lifeCycles": {},
8+
"children": [
9+
{
10+
"componentName": "Text",
11+
"props": {
12+
"style": "display: inline-block;",
13+
"className": "component-base-style",
14+
"text": "测试二级页面使用区块"
15+
},
16+
"children": [],
17+
"id": "66c62533"
18+
},
19+
{
20+
"componentName": "BlockTest1a",
21+
"props": {
22+
"className": "block-base-style"
23+
},
24+
"componentType": "Block",
25+
"children": [],
26+
"id": "75e25225"
27+
}
28+
],
29+
"dataSource": {
30+
"list": []
31+
},
32+
"state": {},
33+
"methods": {},
34+
"utils": [],
35+
"bridge": [],
36+
"inputs": [],
37+
"outputs": [],
38+
"fileName": "BlockPath",
39+
"id": "body"
40+
}

0 commit comments

Comments
 (0)