This article mainly introduces the three writing forms of vue components in detail, which has certain reference value. Interested friends can refer to it, hoping to help everyone.
The first oneUse script tag
<!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>
The second oneUse template tag
<!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>
##The third typeSingle file component
This kind This method is commonly used in Vue single-page applications. For details, please see the official website: https://cn.vuejs.org/v2/guide/single-file-components.html<template> <p class="hello"> <h1>{{ msg }}</h1> </p> </template> <script> export default { name: 'hello', data () { return { msg: '欢迎!' } } } </script>
<!-- 展示模板 --> <template> <p id="app"> <img src="./assets/logo.png"> <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>
How to publish vue components to npm
vue2.0 two-way data binding and form bootstrap+vue component
The above is the detailed content of Detailed explanation of the three writing forms of vue components. For more information, please follow other related articles on the PHP Chinese website!