[英]Can I import x-template script into HTML file?
By default, x-template
(for Vue component) is like默认情况下,
x-template
(用于 Vue 组件)就像
<script type="text/x-template" id="checkbox-template">
<div class="checkbox-wrapper" @click="check">
<div :class="{ checkbox: true, checked: checked }"></div>
<div class="title">{{ title }}</div>
</div>
</script>
Now I want to link an external js file to it, like现在我想将外部 js 文件链接到它,例如
<script type="text/x-template" id="checkbox-template" src="target.js" />
Is it possible?是否可以? If so, what's the syntax for target.js ?
如果是这样, target.js的语法是什么?
No, x-template
must be local.不,
x-template
必须是本地的。 The <script>
element's src
attribute is ignored when the type
attribute is anything other than module
or a JavaScript MIME type, according to MDN :根据MDN ,当
type
属性不是module
或 JavaScript MIME 类型时,将忽略<script>
元素的src
属性:
The embedded content is treated as a data block which won't be processed by the browser.
嵌入的内容被视为浏览器不会处理的数据块。 Developers must use a valid MIME type that is not a JavaScript MIME type to denote data blocks.
开发人员必须使用非 JavaScript MIME 类型的有效 MIME 类型来表示数据块。 The src attribute will be ignored.
src 属性将被忽略。
Some frameworks support an external template through other means (ie templateUrl
, etc) but Vue doesn't.一些框架通过其他方式(即
templateUrl
等)支持外部模板,但 Vue 不支持。 Here is Evan You's (Vue Creator) explanation for why:这是 Evan You (Vue Creator) 对原因的解释:
it's in fact much simpler if things are just in the same file.
如果事情只是在同一个文件中,它实际上要简单得多。 The context switching of jumping back and forth between two files actually makes the development experience much worse
两个文件之间来回跳转的上下文切换其实让开发体验差很多
the number of HTTP requests is still probably the most critical factor in your app's initial load performance.
HTTP 请求的数量可能仍然是影响应用初始加载性能的最关键因素。 Now imagine you use templateURL for every component in your app - the browser needs to perform dozens of HTTP requests before even being able to display anything
现在假设您对应用程序中的每个组件使用 templateURL - 浏览器需要执行数十次 HTTP 请求才能显示任何内容
Of course, you can create your own loader to AJAX the content just like you would load any external file manually, regardless of content, as a string.当然,您可以创建自己的加载器来 AJAX 内容,就像手动加载任何外部文件一样,无论内容如何,都是字符串。 Or you can use the Vue CLI which uses a similar loader.
或者您可以使用使用类似加载器的 Vue CLI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.