Vue学习之x-template

Vue学习之x-template

今天,我们来讲一个比较有趣的一个功能吧
先来看一段代码示例:

 <html>
<head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <my-component></my-component>
        <script type="text/x-template" id="my-component">
            <div>
                <p>This is the content of component</p>
                <p>Hello Vue!</p>
            </div>
            
        </script>
    </div>
        <script>
            Vue.component('my-component',{
                    template:'#my-component'
            });
            var app=new Vue({
                el:"#app"
            });
        </script>
    </body>
    </html>

不知大家有没有注意到,第一个“< script>”中type是x-template。
这是一种比较有用的功能。如果在声明一个组件时,在template中定义模板,如果要换行的话,要加上一个“\”,如果是比较简单的模板还好,如果比较多的话,就会感觉眼花缭乱的,因此我们有一个看起来舒服的方式:x-template

只要写出< script type="text/x-template" id="x-template" >< /script> 在其中间就可以愉快的写HTML代码了。不用考虑换行等问题。这里注意,要加一个id名称,并将其赋给template.然后在声明的组件中加一个:

Vue.component('my-complate',{
                                template:'#x-template'
                               })

不过,Vue的初衷并不是滥用它,因为它将模板与组件的其他定义分离了。因此,我们可以用它来开发一些中小型产品,这是比较方便的。
但据我观察,只能显示处于一个块中的,

<html>
<head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <my-component></my-component>
        <script type="text/x-template" id="my-component">
            
                <div>
                <p>This is the content of component</p>
                <p>Hello Vue!</p>
            </div>
        
        </script>
    </div>
        <script>
            Vue.component('my-component',{
                    template:'#my-component'
            });
            var app=new Vue({
                el:"#app"
            });
        </script>
    </body>
    </html>

以上会显示两行的内容。

以下只会显示第一个< div >标签内容

<html>
<head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <my-component></my-component>
        <script type="text/x-template" id="my-component">
            
                <div>
                <p>This is the content of component</p>
                <p>Hello Vue!</p>
                </div>
                <div>Hello</div>
        </script>
    </div>
        <script>
            Vue.component('my-component',{
                    template:'#my-component'
            });
            var app=new Vue({
                el:"#app"
            });
        </script>
    </body>
    </html>

原因是因为template定义的模板,一定要用一个根元素包裹起来,每个组件必须只有一个根元素,比如上例中,如果去掉< div>标签,那么就相当于有两个根元素。
(如有更详细的解释,请通知我,谢谢:)哈 )

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 10,488评论 0 29
  • # 传智播客vue 学习## 1. 什么是 Vue.js* Vue 开发手机 APP 需要借助于 Weex* Vu...
    再见天才阅读 9,073评论 0 6
  • Vue不支持IE8以及以下版本。 想要使用Vue的话可以通过直接下载vue.js,放置到项目中写好路径就可以,或者...
    酥枫阅读 3,885评论 0 0
  • 转载:http://www.cnblogs.com/huyong/p/6517949.html Vue相关网站参考...
    kathyb24阅读 4,733评论 0 7
  • 发生了蛮多事的。极速的加剧了人的危机感。 看了几本书 看书大概是最能让我静下来的事情。也许是自欺欺人,每次看完都觉...
    三变粥阅读 2,405评论 0 0

友情链接更多精彩内容