This article mainly introduces the three writing forms of vue components in detail, which has certain reference value. Interested friends can refer to it
The example of this article shares the vue component with everyone. The writing form is for your reference. The specific content is as follows
The first typeuses the 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 typeuses the 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 method is commonly used in vue single-page applications. For details, see the official website: https://cn.vuejs.org/v2/guide/single-file-components.html
Create a file with the .vue suffix, component Hello.vue, and place it in the components folder
<template> <p class="hello"> <h1>{{ msg }}</h1> </p> </template> <script> export default { name: 'hello', data () { return { msg: '欢迎!' } } } </script>
app.vue
<!-- 展示模板 --> <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>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to achieve the select-all-cancel effect in JavaScript
##How to achieve the left menu effect using JavaScript
How to implement token verification using vue
How to implement a custom event mechanism using Javascript
The above is the detailed content of What are the ways to write Vue components?. For more information, please follow other related articles on the PHP Chinese website!