首页 / VUE / vue组件的3种书写形式详解
vue组件的3种书写形式详解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue组件的3种书写形式详解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1858字,纯文字阅读大概需要3分钟。
内容图文
![vue组件的3种书写形式详解](/upload/InfoBanner/zyjiaocheng/298/a462988efc184e0b81c5c0b7f8e9ebe2.jpg)
第一种使用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>{{ 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>
相关推荐:
Vue组件选项props
vue组件如何发布到npm
vue2.0数据双向绑定与表单bootstrap+vue组件
以上就是vue组件的3种书写形式详解的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的vue组件的3种书写形式详解全部内容,希望文章能够帮你解决vue组件的3种书写形式详解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。