vue中template的三种写法示例


Posted in Javascript onOctober 21, 2020

第一种(字符串模板写法):

直接写在vue构造器里,这种写法比较直观,适用于html代码不多的场景,但是如果模板里html代码太多,不便于维护,不建议这么写.

<!DOCTYPE html>
<html>
 <!--
  WARNING! Make sure that you match all Quasar related
  tags to the same version! (Below it's "@1.7.4")
 -->

 <head>
   <!--
   <link href="https://cdn.jsdelivr.net/npm/quasar@1.7.4/dist/quasar.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css">
   -->
   <link href="https://www.mobibrw.com/wp-content/uploads/2020/06/quasar@1.7.4.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css">
 </head>

 <body>
  <div id="q-app">

  </div>

  <!-- Add the following at the end of your body tag -->
  <!--
  <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/quasar@1.7.4/dist/quasar.umd.min.js"></script>
  -->
  <script src="https://www.mobibrw.com/wp-content/uploads/2020/06/vue@2.0.0.js"></script>
  <script src="https://www.mobibrw.com/wp-content/uploads/2020/06/quasar.umd@1.7.4.js"></script>
  
  <script>
   /*
    Example kicking off the UI. Obviously, adapt this to your specific needs.
    Assumes you have a <div id="q-app"></div> in your <body> above
    */
   new Vue({
    el: '#q-app',
    data: function () {
     return {
      version: Quasar.version
     }
    },
    template: `<div class="q-ma-md"> Running Quasar v{{ version }}</div>`
    // ...etc
   })
  </script>
 </body>
</html>

第二种:

直接写在template标签里,这种写法跟写html很像。

<!DOCTYPE html>
<html>
 <!--
  WARNING! Make sure that you match all Quasar related
  tags to the same version! (Below it's "@1.7.4")
 -->

 <head>
   <!--
   <link href="https://cdn.jsdelivr.net/npm/quasar@1.7.4/dist/quasar.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css">
   -->
   <link href="https://www.mobibrw.com/wp-content/uploads/2020/06/quasar@1.7.4.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css">
 </head>

 <body>
  <div id="q-app">
    <template id='template1'>
     <div class="q-ma-md">
      Running Quasar v{{ version }}
     </div>
    </template>
  </div>

  <!-- Add the following at the end of your body tag -->
  <!--
  <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/quasar@1.7.4/dist/quasar.umd.min.js"></script>
  -->
  <script src="https://www.mobibrw.com/wp-content/uploads/2020/06/vue@2.0.0.js"></script>
  <script src="https://www.mobibrw.com/wp-content/uploads/2020/06/quasar.umd@1.7.4.js"></script>
  
  <script>
   /*
    Example kicking off the UI. Obviously, adapt this to your specific needs.
    Assumes you have a <div id="q-app"></div> in your <body> above
    */
   new Vue({
    el: '#q-app',
    data: function () {
     return {
      version: Quasar.version
     }
    },
    template: '#template1'
    // ...etc
   })
  </script>
 </body>
</html>

第三种:

写在script标签里,这种写法官方推荐,vue官方推荐script中type属性加上"x-template",即:

<!DOCTYPE html>
<html>
 <!--
  WARNING! Make sure that you match all Quasar related
  tags to the same version! (Below it's "@1.7.4")
 -->

 <head>
   <!--
   <link href="https://cdn.jsdelivr.net/npm/quasar@1.7.4/dist/quasar.min.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css">
   -->
   <link href="https://www.mobibrw.com/wp-content/uploads/2020/06/quasar@1.7.4.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css">
 </head>

 <body>
  <div id="q-app"></div>
	
	<script type="x-template" id="template1">
  	<div class="q-ma-md">
   	 Running Quasar v{{ version }}
  	</div>
  </script>

  <!-- Add the following at the end of your body tag -->
  <!--
  <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/quasar@1.7.4/dist/quasar.umd.min.js"></script>
  -->
  <script src="https://www.mobibrw.com/wp-content/uploads/2020/06/vue@2.0.0.js"></script>
  <script src="https://www.mobibrw.com/wp-content/uploads/2020/06/quasar.umd@1.7.4.js"></script>
  
  <script>
   /*
    Example kicking off the UI. Obviously, adapt this to your specific needs.
    Assumes you have a <div id="q-app"></div> in your <body> above
    */
   new Vue({
    el: '#q-app',
    data: function () {
     return {
      version: Quasar.version
     }
    },
    template: '#template1'
    // ...etc
   })
  </script>
 </body>
</html>

以上就是vue中template的三种写法示例的详细内容,更多关于vue template的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
浅析onsubmit校验表单时利用ajax的return false无效问题
Jul 10 Javascript
JavaScript通过字符串调用函数的实现方法
Mar 18 Javascript
jQuery基于muipicker实现仿ios时间选择
Feb 22 Javascript
把多个JavaScript函数绑定到onload事件处理函数上的方法
Sep 04 Javascript
Vue.js实现一个todo-list的上移下移删除功能
Jun 26 Javascript
详解Angular操作cookies方法
Jun 01 Javascript
微信小程序实现人脸识别登陆的示例代码
Apr 02 Javascript
10个最受欢迎的 JavaScript框架(推荐)
Apr 24 Javascript
javascript实现5秒倒计时并跳转功能
Jun 20 Javascript
简单了解vue中父子组件如何相互传递值(基础向)
Jul 12 Javascript
微信小程序实现发微博功能的示例代码
Jun 24 Javascript
vue data有值,但是页面{{}} 取不到值的解决
Nov 09 Javascript
Vue使用v-viewer实现图片预览
Oct 21 #Javascript
UEditor 自定义图片视频尺寸校验功能的实现代码
Oct 20 #Javascript
Vue+axios封装请求实现前后端分离
Oct 23 #Javascript
构建一个JavaScript插件系统
Oct 20 #Javascript
vue中解决微信html5原生ios虚拟键返回不刷新问题
Oct 20 #Javascript
原生js实现俄罗斯方块
Oct 20 #Javascript
React实现评论的添加和删除
Oct 20 #Javascript
You might like
使用MaxMind 根据IP地址对访问者定位
2006/10/09 PHP
PHP 冒泡排序算法的实现代码
2010/08/08 PHP
关于php 接口问题(php接口主要也就是运用curl,curl函数)
2013/07/01 PHP
php技术实现加载字体并保存成图片
2015/07/27 PHP
WordPress中设置Post Type自定义文章类型的实例教程
2016/05/10 PHP
利用jQuary实现文字浮动提示效果示例代码
2013/12/26 Javascript
JS实现很酷的水波文字特效实例
2015/02/26 Javascript
JavaScript中的对象与JSON
2015/07/03 Javascript
Bootstrap表格和栅格分页实例详解
2016/05/20 Javascript
再谈Javascript中的异步以及如何异步
2016/08/19 Javascript
Easyui ueditor 整合解决不能编辑的问题(推荐)
2017/06/25 Javascript
Bootstrap table表格初始化表格数据的方法
2018/07/25 Javascript
vue 项目 iOS WKWebView 加载
2019/04/17 Javascript
javascript事件监听与事件委托实例详解
2019/08/16 Javascript
jQuery操作事件完整实例分析
2020/01/10 jQuery
学习 Vue.js 遇到的那些坑
2021/02/02 Vue.js
pycharm 使用心得(八)如何调用另一文件中的函数
2014/06/06 Python
Python爬虫框架Scrapy实例代码
2018/03/04 Python
numpy中以文本的方式存储以及读取数据方法
2018/06/04 Python
Python3多进程 multiprocessing 模块实例详解
2018/06/11 Python
Python在for循环中更改list值的方法【推荐】
2018/08/17 Python
Django实现一对多表模型的跨表查询方法
2018/12/18 Python
详解利用Python scipy.signal.filtfilt() 实现信号滤波
2019/06/05 Python
详解HTML5 canvas绘图基本使用方法
2018/01/29 HTML / CSS
allbeauty美国:英国在线美容店
2019/03/11 全球购物
编写一子程序,将一链表倒序,即使链表表尾变表头,表头变表尾
2016/02/10 面试题
会计岗位职责
2013/11/08 职场文书
韩国商务邀请函
2014/01/14 职场文书
优秀管理者获奖感言
2014/02/17 职场文书
2014新年元旦活动策划方案
2014/02/18 职场文书
《蚂蚁和蝈蝈》教学反思
2014/02/24 职场文书
入党自我鉴定
2014/03/25 职场文书
小学安全工作汇报材料
2014/08/19 职场文书
MySQL慢查询中的commit慢和binlog中慢事务的区别
2022/06/16 MySQL
Redis实现主从复制方式(Master&Slave)
2022/06/21 Redis
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技