Skip to content

[vue] <template></template>有什么用? #439

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

[vue] <template></template>有什么用?

Activity

maozhuo123

maozhuo123 commented on Jul 3, 2019

@maozhuo123

当做一个不可见的包裹元素,减少不必要的DOM元素,整个结构会更加清晰。

censek

censek commented on Oct 29, 2019

@censek

被当做一个不可见的包裹元素,主要用于分组的条件判断和列表渲染。
https://blog.csdn.net/u013594477/article/details/80774483

xiao-fe

xiao-fe commented on May 25, 2020

@xiao-fe

包裹嵌套其它元素,使元素具有区域性,自身具有三个特点:
*隐藏性:不会显示在页面中
*任意性:可以写在页面的任意地方
*无效性: 没有一个根元素包裹,任何HTML内容都是无效的

radio-qq

radio-qq commented on Jan 1, 2021

@radio-qq

template作用:相当于一个占位符,不显示在页面中,减少了不必要的dom元素
场景:结合v-for、v-if等一起使用,插槽时使用

hyj443

hyj443 commented on Oct 20, 2021

@hyj443
<div>
    <template v-for="(item, i) in arr" :key="item.id">
        <div>{{item.text}}</div>
        <span>{{item.text}}</span>
    </template>
</div>

这么写,就不会额外增加一个div了,template标签就是一个模板占位符,编译的过程中,不会渲染成元素,用来帮我们包裹元素

还有一个优点

可以把 v-if v-for 等指令写在template标签中,把事件绑定写在template的子元素中,这样html结构就更清晰,标签数量不会过长。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@maozhuo123@censek@hyj443@radio-qq

        Issue actions

          [vue] `<template></template>`有什么用? · Issue #439 · haizlin/fe-interview