简体   繁体   English

我可以将 x-template 脚本导入 HTML 文件吗?

[英]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.

相关问题 Sublime Text 3 中 x-template 脚本标签内 HTML 的语法高亮显示 - Syntax Highlighting for HTML inside x-template script tags in Sublime Text 3 Vue JS 组件如何在 Twig x-template 文件中工作 - How the Vue JS component work in Twig x-template file VueJS:对组件内部的子组件使用x-template - VueJS: Using x-template for a sub-component inside a component Rails 7 导入地图 - 如何将脚本注入 HTML 模板? - Rails 7 Import Maps - How do I inject a script into HTML template? 我可以导入带有<script> tag with html webpack plugin? - Can I import a script with a <script> tag with html webpack plugin? Vue.js 组件:作为 object 的道具不适用于 x-template - Vue.js component: Props as object doesn’t work with x-template 将text.js与require.js结合使用时,如何创建一个模板html文件,但分别获取每个脚本模板? - Using text.js with require.js how can I create one template html file but grab each script template individually? 从聚合物3中的单独的html文件导入模板 - Import template from separate html file in polymer 3 如何在typescript中导入template.html - how can import template.html in typescript 如何使用 webpack 导入 HTML 文件? - How can I import an HTML file using webpack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM