一、為什麼需要使用Vue template標籤
1、Vue template標籤可以聲明組件的模板,在Vue實例中進行重用:
<template id="my-template"> <div> <p>{{ message }}</p> </div> </template> <script> new Vue({ el: '#app', components: { 'my-component': { template: '#my-template', data: function() { message: 'Hello World' } } } }) </script>
2、Vue template標籤還可以用於條件渲染,例如:
<template v-if="show"> <p>Only show when "show" is true</p> </template>
3、使用Vue template標籤可以很輕鬆地定義循環渲染的列表:
<template v-for="item in items"> <li>{{ item }}</li> </template>
二、Vue template標籤的嵌套使用
1、Vue template標籤可以嵌套使用,支持組件化開發:
<template id="child-template"> <div> <p>{{ message }}</p> </div> </template> <template id="parent-template"> <child-component></child-component> </template> <script> Vue.component('child-component', { template: '#child-template', data: function() { return { message: 'Hello from child component' } } }); new Vue({ el: '#app', template: '#parent-template' }); </script>
2、嵌套使用Vue template標籤還可以實現複雜邏輯的組件嵌套渲染:
<template id="grandchild-template"> <div> <p>{{ message }}</p> </div> </template> <template id="child-template"> <div> <p>{{ message }}</p> <grandchild-component></grandchild-component> </div> </template> <template id="parent-template"> <div> <p>{{ message }}</p> <child-component></child-component> </div> </template> <script> Vue.component('grandchild-component', { template: '#grandchild-template', data: function() { return { message: 'Hello from grandchild component' } } }); Vue.component('child-component', { template: '#child-template', data: function() { return { message: 'Hello from child component' } } }); new Vue({ el: '#app', template: '#parent-template', data: function() { return { message: 'Hello from parent component' } } }); </script>
三、Vue template標籤的作用域
1、在Vue模板中,數據被封裝在組件的作用域內,可以使用{{}}語法讀取:
<template> <div> <p>{{ message }}</p> </div> </template> <script> Vue.component('my-component', { data: function() { return { message: 'Hello from my component' } } }); new Vue({ el: '#app', components: { 'my-component': 'my-component' }, template: '<my-component></my-component>' }); </script>
2、可以使用特殊的屬性$v-on和$v-bind來綁定組件中的數據:
<template> <div> <button v-bind:class="className" v-on:click="changeClass">{{ buttonText }}</button> </div> </template> <script> Vue.component('my-button', { data: function() { return { className: 'btn-primary', buttonText: 'Click Me' } }, methods: { changeClass: function() { this.className = 'btn-danger'; this.buttonText = 'Changed!'; } } }); new Vue({ el: '#app', components: { 'my-button': 'my-button' }, template: '<my-button></my-button>' }); </script>
四、Vue template標籤的語法糖
1、可以在Vue模板中使用特殊的語法糖,如v-show、v-if、v-for等等:
<template> <div> <p v-show="showMessage">{{ message }}</p> </div> </template> <script> Vue.component('my-component', { data: function() { return { showMessage: true, message: 'Hello World' } } }); new Vue({ el: '#app', components: { 'my-component': 'my-component' }, template: '<my-component></my-component>' }); </script>
2、還可以使用Vue的計算屬性來實現更高級的語法糖:
<template> <div> <p v-bind:class="['btn', colorClass]">{{ buttonText }}</p> </div> </template> <script> Vue.component('my-button', { data: function() { return { buttonColor: 'primary', buttonText: 'Click Me' } }, computed: { colorClass: function() { return 'btn-' + this.buttonColor; } } }); new Vue({ el: '#app', components: { 'my-button': 'my-button' }, template: '<my-button></my-button>' }); </script>
五、Vue template標籤的局限性
1、由於Vue模板只是字符串,沒有真正的JavaScript語法,因此不能像Vue的JavaScript代碼那樣進行類型檢查和編譯時錯誤檢查。
2、Vue模板雖然可以動態生成,但是難以維護和優化,尤其是一些複雜的模板結構。
3、Vue模板的性能略差於手寫的JavaScript代碼。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/259725.html