Blogger Information
Blog 70
fans 1
comment 0
visits 53101
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
课上vue指令全部上机操作
葡萄枝子
Original
658 people have browsed it

课上vue指令全部上机操作

将课上提及的vue指令全部上机操作并发布

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

vue指令

vue指令2

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