国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue組件模板的幾種書寫形式(3種)

2024-05-06 15:44:39
字體:
來源:轉載
供稿:網友

1.第一種使用script標簽

<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title></title>    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  </head>  <body>    <div id="app">      <test-component></test-component>    </div>    <script type="text/x-template" id="testComponent"><!-- 注意 type 和id。 -->      <div>{{test}} look test component!</div>    </script>  </body>  <script>    //全局注冊組件    Vue.component('test-component',{      template: '#testComponent',       data(){       return{        test:"hello"       }      }    })    new Vue({      el: '#app'    })  </script></html>

注意:使用<script>標簽時,type指定為text/x-template,意在告訴瀏覽器這不是一段js腳本,
瀏覽器在解析HTML文檔時會忽略<script>標簽內定義的內容。

2.第二種使用template標簽

<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title></title>    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>  </head>  <body>    <div id="app">      <test-component></test-component>    </div>    <template id="testComponent">      <div>look test component!</div>    </template>  </body>  <script>    Vue.component('test-component',{      template: '#testComponent'    })    new Vue({      el: '#app'    })  </script></html>

當然,如果template內容少的話,我們可以直接在組件中書寫,而不需要用template標簽。像下面這樣:

 Vue.component('test-component',{    template:`<h1>this is test,{{test}}</h1>`,    data(){     return{       test:"hello test"       }      }  })

3.第三種 單文件組件

這種方法常用在vue單頁應用中

創建.vue后綴的文件,組件Hello.vue,放到components文件夾中

<template> <div class="hello">  <h1>{{ msg }}</h1> </div></template><script>export default { name: 'hello', data () {  return {   msg: '歡迎!'  } }}</script>app.vue<template> <div id="app">  <img src="./assets/logo.png">  <hello></hello> </div></template><script>import Hello from './components/Hello'export default { name: 'app', components: {  Hello }}</script><style>#app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px;}</style>

以上就是Vue組件模板的幾種書寫形式(3種)的詳細內容,更多關于Vue 組件模板請關注錯新站長站其它相關文章!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 堆龙德庆县| 阿拉尔市| 白水县| 庄河市| 舒城县| 濮阳市| 蓬溪县| 双鸭山市| 巴里| 庆元县| 本溪| 延边| 天全县| 织金县| 大城县| 平顺县| 屯留县| 凯里市| 静安区| 象州县| 陵川县| 高碑店市| 蒙山县| 平度市| 友谊县| 遵义县| 大安市| 思南县| 九龙县| 碌曲县| 滨州市| 武宣县| 洪湖市| 古丈县| 宁德市| 宁远县| 泊头市| 武穴市| 吉林省| 香河县| 富锦市|