Blogger Information
Blog 47
fans 3
comment 0
visits 38270
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue基础指令实例演示
Original
569 people have browsed it

vue基础指令实例演示

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>课上vue指令上机操作</title>
  6. <style>
  7. .active {
  8. color: violet;
  9. }
  10. .desc {
  11. text-decoration: underline;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- 引入vue框架 -->
  17. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  18. <div class="app">
  19. <!-- 使用插值 -->
  20. <h2>{{title}}</h2>
  21. <p>{{content}}</p>
  22. <h3>v-text/v-html/v-once指令</h3>
  23. <ul>
  24. <!-- 渲染title变量纯文本 -->
  25. <li v-text="title"></li>
  26. <!-- 渲染content变量为html -->
  27. <li v-html="content"></li>
  28. <!-- 只渲染一次,后面改变实例的属性title就不再显示 -->
  29. <li v-once="title">{{title}}</li>
  30. </ul>
  31. <h3>绑定属性/事件</h3>
  32. <ul>
  33. <!-- 内联样式 -->
  34. <li v-once v-bind:style="style">{{title}}</li>
  35. <!-- 邦定css类 -->
  36. <li v-once v-bind:class="className">{{title}}</li>
  37. <!-- 简写方式绑定css -->
  38. <li v-once :class="className">{{title}}</li>
  39. <!-- 简写数组方式绑定css -->
  40. <li v-once :class="[`active`,`desc`]">{{title}}</li>
  41. <!-- 简写对象方式绑定css -->
  42. <li v-once :class="{active:false,desc:true}">{{title}}</li>
  43. <!-- 简写对象方式,含变量绑定css -->
  44. <li v-once :class="{active:isActive,desc:isdesc}">{{title}}</li>
  45. <!-- 点击count变量+1 -->
  46. <li v-on:click="addCount"><button>count+1</button> {{conut}}</li>
  47. <!-- 简写点击事件count自增 -->
  48. <li @click="addCount"><button>count+1</button> {{conut}}</li>
  49. <!-- 禁止超链接默认访问 -->
  50. <li><a href="https://php.cn" @click.prevent="addCount">php中文网 {{conut}}</a></li>
  51. <!-- 只执行一次自增,点第二次不再+1 -->
  52. <li><a href="JavaScript:" @click.once="addCount">php中文网 {{conut}}</a></li>
  53. <!-- 事件方法 -->
  54. <li><button @click.stop="handle($event, 1, 2)">click</button> {{handleText}}</li>
  55. <!-- 双向绑定 -->
  56. <li><input type="text" :value="site" @inpit="site=$event.target.value">{{site}}</li>
  57. <!-- 双向绑定,回车后同步 -->
  58. <li><input type="text" v-model.lazy="site">{{site}}</li>
  59. </ul>
  60. <ul>
  61. <!-- 设置唯一key让vue不需要重复渲染 -->
  62. <!-- 渲染数组 -->
  63. <li v-for="(item,index) in items" :key="index">{{index}}---{{item}}</li>
  64. </ul>
  65. <ul>
  66. <!-- 渲染对象 -->
  67. <li v-for="(item,prop,index) in user" :key="prop">{{prop}}---{{index}}---{{item}}</li>
  68. </ul>
  69. <ul>
  70. <!-- 渲染对象数组 -->
  71. <li v-for="(item,index) in users" :key="item.id">{{index}}---{{item.name}}---{{item.email}}</li>
  72. </ul>
  73. <ul>
  74. <li v-for="(n,i) in 6">{{i}}---{{n}}</li>
  75. </ul>
  76. </div>
  77. <script>
  78. // 创建一个vue实例
  79. const vm = new Vue({
  80. // 挂载点
  81. el: '.app',
  82. // 数据注入
  83. data: {
  84. title: 'Hello Wrold!',
  85. content: '<strong><em>content</em></strong>',
  86. style: 'color:red',
  87. className: 'active desc',
  88. isActive: false,
  89. isdesc: true,
  90. conut: 0,
  91. handleText: 'handle',
  92. site: 'php.cn',
  93. // 数组
  94. items: ["电脑", "手机", "笔记本"],
  95. // 对象
  96. user: {
  97. name: "天蓬",
  98. email: "tp@php.cn",
  99. },
  100. // 对象数组, 数据表的查询结果就是一个二个这样的二维的JSON
  101. users: [
  102. { id: 1, name: "天蓬", email: "tp@php.cn" },
  103. { id: 2, name: "灭绝", email: "mj@php.cn" },
  104. { id: 3, name: "西门", email: "xm@php.cn" },
  105. ],
  106. },
  107. // 注册方法
  108. methods: {
  109. addCount: function () {
  110. return this.conut += 1
  111. },
  112. handle(ev, a, b) {
  113. this.handleText = (`${ev.type} ${a} + ${b} = ${a + b}`);
  114. }
  115. },
  116. })
  117. // 返回hello world
  118. console.log(vm.title);
  119. // 改变vm实例的属性title
  120. vm.title = "天蓬大人";
  121. console.log(vm.title);
  122. </script>
  123. </body>
  124. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments