JS中内联模板字符串是指什么?

链接描述

https://img4.mukewang.com/5c8de86b0001564f08000298.jpg

在Vuejs的入门文档中看到这个,有点不太理解,我试过用反引号,但是会报错,又去搜索所谓内联模板字符串,也没有发现完全匹配的答案.求指导.

慕桂英4014372
浏览 855回答 4
4回答

手掌心

大神们不要把问题复杂化好不啦受到 HTML 本身的一些限制,像 <ul>、<ol>、<table>、<select> 这样的元素里允许包含的元素有限制,而另一些像 <option> 这样的元素只能出现在某些特定元素的内部。变通的方案是使用特殊的 is 特性应当注意,如果使用来自以下来源之一的字符串模板,则没有这些限制:-----------<script type="text/x-template">-----------JavaScript 内联模板字符串-----------.vue 组件这句话意思是:这样不可以<body>&nbsp; &nbsp; <div id="app">&nbsp; &nbsp; &nbsp; &nbsp; <select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <optioncomp></optioncomp>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="lib/vue.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; new Vue({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el: '#app',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; components:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'optioncomp':{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<option >a</option>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script></body>但是用is特殊属性可以:<body>&nbsp; &nbsp; <div id="app">&nbsp; &nbsp; &nbsp; &nbsp; <select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option is="optioncomp"></option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="lib/vue.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; new Vue({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el: '#app',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; components:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'optioncomp':{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<option >a</option>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script></body>或者temp模板标签也可以<body>&nbsp; &nbsp; <div id="app">&nbsp; &nbsp; &nbsp; &nbsp; <select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option is="optioncomp"></option>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; <!--模板内容存放区域-->&nbsp; &nbsp; &nbsp; &nbsp; <script type="x-template" id="optioncompTemp">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option >a</option>&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="lib/vue.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; new Vue({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el: '#app',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; components:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'optioncomp':{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '#optioncompTemp'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script></body>或者内联模板字符串也行<body>&nbsp; &nbsp; <div id="app">&nbsp; &nbsp; &nbsp; &nbsp; <selectcomp></selectcomp>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="lib/vue.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; Vue.component('optioncomp',{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: '<option >a</option>'&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; new Vue({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el: '#app',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; components:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'selectcomp':{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template: ' <select> <optioncomp></optioncomp></select>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script></body>当然了,单页应用的组件文件xxx.vue更是没问题了,就不演示了

HUWWW

类似于下面这种,可能含有mustache表达式的,同时支持es6模板字符串语法。<div id="app">&nbsp; &nbsp; <checkbox-test></checkbox-test></div></body><script>&nbsp; &nbsp; var content="content-test";&nbsp; &nbsp; Vue.component('checkbox-test', {&nbsp; &nbsp; &nbsp; &nbsp; template: `<div&nbsp; @click="check">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="title">{{ title }}</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="title">${content}</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>`,&nbsp; &nbsp; &nbsp; &nbsp; data() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return { checked: false, title: 'title-test' }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; methods: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; check() { this.checked = !this.checked; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; window.vm=new Vue({&nbsp; &nbsp; &nbsp; &nbsp; el:"#app",&nbsp; &nbsp; });</script>页面效果

紫衣仙女

//内联模板字符串Vue.component('component1',{&nbsp; &nbsp; template: '<tr><td>child component</td></tr>'});Vue.component('component2',{&nbsp; &nbsp; template: '<table><component1></component1></table>'});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript