這篇文章主要為大家詳細介紹了3種vue組件的書寫形式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
#本文實例為大家分享了vue組件的書寫形式,供大家參考,具體內容如下
第一種使用script標籤
<!DOCTYPE html> <html> <body> <p id="app"> <my-component></my-component> </p> <-- 注意:使用<script>标签时,type指定为text/x-template,意在告诉浏览器这不是一段js脚本,浏览器在解析HTML文档时会忽略<script>标签内定义的内容。--> <script type="text/x-template" id="myComponent">//注意 type 和id。 <p>This is a component!</p> </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> <p id="app"> <my-component></my-component> </p> <template id="myComponent"> <p>This is a component!</p> </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> <p class="hello"> <h1 id="nbsp-msg-nbsp">{{ msg }}</h1> </p> </template> <script> export default { name: 'hello', data () { return { msg: '欢迎!' } } } </script>
app.vue
<!-- 展示模板 --> <template> <p id="app"> <img src="/static/imghwm/default1.png" data-src="./assets/logo.png" class="lazy" alt="有關vue組件的書寫方式有哪些?" > <hello></hello> </p> </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組件的書寫方式有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。