国产探花免费观看_亚洲丰满少妇自慰呻吟_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 組件模板請關注錯新站長站其它相關文章!

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

圖片精選

主站蜘蛛池模板: 崇义县| 武川县| 建阳市| 宜丰县| 凤庆县| 西丰县| 眉山市| 南丰县| 梁平县| 临泽县| 上饶市| 涞源县| 买车| 石屏县| 巫溪县| 容城县| 华安县| 宽甸| 奎屯市| 平和县| 福海县| 普兰店市| 广水市| 成安县| 固安县| 彩票| 宣威市| 乡城县| 从江县| 汉阴县| 汉中市| 洛扎县| 紫金县| 乌兰浩特市| 丰台区| 江津市| 陆良县| 米脂县| 隆子县| 荥经县| 丹巴县|