Vue组件模板的几种书写形式(3种)
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了Vue组件模板的几种书写形式(3种),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
1.第一种使用script标签
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.jsdelivr./npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <test-ponent></test-ponent> </div> <script type="text/x-template" id="testComponent"><!-- 注意 type 和id。 --> <div>{{test}} look test ponent!</div> </script> </body> <script> //全局注册组件 Vue.ponent('test-ponent',{ template: '#testComponent', data(){ return{ test:"hello" } } }) new Vue({ el: '#app' }) </script> </html>
注意使用<script>标签时,type指定为text/x-template,意在告诉浏览器这不是一段js脚本,
浏览器在解析HTML文档时会忽略<script>标签内定义的内容。
2.第二种使用template标签
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.jsdelivr./npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <test-ponent></test-ponent> </div> <template id="testComponent"> <div>look test ponent!</div> </template> </body> <script> Vue.ponent('test-ponent',{ template: '#testComponent' }) new Vue({ el: '#app' }) </script> </html>
,如果template内容少的话,我们可以直接在组件中书写,而不需要用template标签。像狼蚁网站SEO优化这样
Vue.ponent('test-ponent',{ template:`<h1>this is test,{{test}}</h1>`, data(){ return{ test:"hello test" } } })
3.第三种 单文件组件
这种方法常用在vue单页应用中
创建.vue后缀的文件,组件Hello.vue,放到ponents文件夹中
<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 './ponents/Hello' export default { name: 'app', ponents: { 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-: 60px; } </style>
以上就是Vue组件模板的几种书写形式(3种)的详细内容,更多关于Vue 组件模板请关注狼蚁SEO其它相关文章!
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南