Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromvuejs/vue

Commitecac831

Browse files
kballyyx990803
authored andcommitted
fix(compiler): templates inside v-pre should be rendered to HTML (vuejs#8146)
closevuejs#8041
1 parent61c32cc commitecac831

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

‎src/compiler/codegen/index.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class CodegenState {
1818
maybeComponent:(el:ASTElement)=>boolean;
1919
onceId:number;
2020
staticRenderFns:Array<string>;
21+
pre:boolean;
2122

2223
constructor(options:CompilerOptions){
2324
this.options=options
@@ -29,6 +30,7 @@ export class CodegenState {
2930
this.maybeComponent=(el:ASTElement)=>!(isReservedTag(el.tag)&&!el.component)
3031
this.onceId=0
3132
this.staticRenderFns=[]
33+
this.pre=false
3234
}
3335
}
3436

@@ -58,7 +60,7 @@ export function genElement (el: ASTElement, state: CodegenState): string {
5860
returngenFor(el,state)
5961
}elseif(el.if&&!el.ifProcessed){
6062
returngenIf(el,state)
61-
}elseif(el.tag==='template'&&!el.slotTarget){
63+
}elseif(el.tag==='template'&&!el.slotTarget&&!state.pre){
6264
returngenChildren(el,state)||'void 0'
6365
}elseif(el.tag==='slot'){
6466
returngenSlot(el,state)
@@ -88,7 +90,15 @@ export function genElement (el: ASTElement, state: CodegenState): string {
8890
// hoist static sub-trees out
8991
functiongenStatic(el:ASTElement,state:CodegenState):string{
9092
el.staticProcessed=true
93+
// Some elements (templates) need to behave differently inside of a v-pre
94+
// node. All pre nodes are static roots, so we can use this as a location to
95+
// wrap a state change and reset it upon exiting the pre node.
96+
constoriginalPreState=state.pre
97+
if(el.pre){
98+
state.pre=el.pre
99+
}
91100
state.staticRenderFns.push(`with(this){return${genElement(el,state)}}`)
101+
state.pre=originalPreState
92102
return`_m(${
93103
state.staticRenderFns.length-1
94104
}${

‎test/unit/modules/compiler/codegen.spec.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,18 @@ describe('codegen', () => {
595595
expect(res.render).toBe(generatedCode)
596596
})
597597

598+
// #8041
599+
it('does not squash templates inside v-pre',()=>{
600+
consttemplate='<div v-pre><template><p>{{msg}}</p></template></div>'
601+
constgeneratedCode=`with(this){return _m(0)}`
602+
constrenderFn=`with(this){return _c('div',{pre:true},[_c('template',[_c('p',[_v("{{msg}}")])])],2)}`
603+
constast=parse(template,baseOptions)
604+
optimize(ast,baseOptions)
605+
constres=generate(ast,baseOptions)
606+
expect(res.render).toBe(generatedCode)
607+
expect(res.staticRenderFns).toEqual([renderFn])
608+
})
609+
598610
it('not specified ast type',()=>{
599611
constres=generate(null,baseOptions)
600612
expect(res.render).toBe(`with(this){return _c("div")}`)

‎test/unit/modules/compiler/parser.spec.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ describe('parser', () => {
250250
expect(ast.children[0].children[0].text).toBe('{{msg}}')
251251
})
252252

253+
it('v-pre directive should leave template in DOM',()=>{
254+
constast=parse('<div v-pre id="message1"><template id="template1"><p>{{msg}}</p></template></div>',baseOptions)
255+
expect(ast.pre).toBe(true)
256+
expect(ast.attrs[0].name).toBe('id')
257+
expect(ast.attrs[0].value).toBe('"message1"')
258+
expect(ast.children[0].attrs[0].name).toBe('id')
259+
expect(ast.children[0].attrs[0].value).toBe('"template1"')
260+
})
261+
253262
it('v-for directive basic syntax',()=>{
254263
constast=parse('<ul><li v-for="item in items"></li></ul>',baseOptions)
255264
constliAst=ast.children[0]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp