3種vue組件的書寫形式,vue組件書寫

來源:互聯網
上載者:User

3種vue組件的書寫形式,vue組件書寫

本文執行個體為大家分享了vue組件的書寫形式,供大家參考,具體內容如下

第一種使用script標籤

<!DOCTYPE html><html>  <body>    <div id="app">      <my-component></my-component>    </div>    <-- 注意:使用<script>標籤時,type指定為text/x-template,意在告訴瀏覽器這不是一段js指令碼,瀏覽器在解析HTML文檔時會忽略<script>標籤內定義的內容。-->    <script type="text/x-template" id="myComponent">//注意 type 和id。      <div>This is a component!</div>    </script>  </body>  <script src="js/vue.js"></script>  <script>    //全域註冊組件    Vue.component('my-component',{      template: '#myComponent'    })    new Vue({      el: '#app'    })  </script></html>

第二種使用template標籤

<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title></title>  </head>  <body>    <div id="app">      <my-component></my-component>    </div>    <template id="myComponent">      <div>This is a component!</div>    </template>  </body>  <script src="js/vue.js"></script>  <script>    Vue.component('my-component',{      template: '#myComponent'    })    new Vue({      el: '#app'    })  </script></html>

第三種 單檔案組件

這種方法常用在vue單頁應用中。詳情看官網:https://cn.vuejs.org/v2/guide/single-file-components.html

建立.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>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.