vue的X-Templates

当组件模板内容冗长复杂时,在JavaScript里拼接字符串效率低。Vue提供在< script >标签里使用text/x-template类型并指定id,将id赋给template的方式定义模板,可在< script >标签里愉快写html代码,无需考虑换行等问题。

当你写的组件模板template内容很冗长和复杂时,如果都在JavaScript里拼接字符串,效率是很低的,因为不像直接写html简单。
为了解决这个问题,Vue提供了另一种定义模板的方式,在< script >标签里使用text/x-template类型,并且指定一个id,将这个id赋给template。代码如下

<div id="app">
	<my-component></my-component>
	<script type="text/x-template" id="my-component">
		<div>这是组件的内容</div>
	</script>
</div>
<script>
	Vue.component('my-component',{
		template:'#my-component'
	}),
	vat app = new Vue({
		el:"#app"
	})
</script>

运行效果如下图:
在这里插入图片描述
在< script >标签里,就可以愉快地写html代码而不用考虑换行等问题了

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值