Vue之 x-templates

Vue.js 中的x-template用法解析
Vue在组件内容复杂时提供了一种x-template定义模板的方法,通过在script标签中指定id,提高代码可读性和编码效率。这种方式将模板与组件其他定义分离,但Vue并不推荐过度使用。在使用编译工具处理后,可以更好地处理HTML编写问题。

在组件template内容很冗长、复杂的情况下,如果在Javascript中拼接字符串,代码的可读性,以及编码效率都是很低的。Vue提供了另外一种定义模板的方式,在<script>标签中使用text/x-template类型,并且指定一个id,将这个id赋给template

<!-- x-template -->
<body>
    <div id="app">
        <my-component></my-component>
        <!-- 使用x-template -->
        <script type="text/x-template" id="my-component">
            <div>这是组件的内容</div>
        </script>
    </div>

    <script src = "https://unpkg.com/vue/dist/vue.min.js"></script>
    <script>
        Vue.component('my-component', {
            template: '#my-component'   //将id赋给template
        });

        var app = new Vue({
            el: '#app',
        })
    </script>
</body>

这种方法将模板和组件的其他定义隔离了,Vue不建议滥用。如果使用编译工具来编译.vue文件后,就可以更好的解决HTML书写的问题。

参考

  1. 《Vue.js 实战》
zy@zy-VMware-Virtual-Platform:~/zy/project-zy$ docker-compose logs -f vue-app vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:25:42 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:25:51 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:25:59 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:08 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:17 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:26 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:38 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:52 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:27:13 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:27:47 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:28:46 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:29:54 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:31:03 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:32:11 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:33:19 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:34:27 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:35:35 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:36:43 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值