Blogger Information
Blog 29
fans 0
comment 0
visits 18415
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
v-for注意事项、键盘修饰符enter事件、vue生命周期解释、计算属性get、set、watch
CC
Original
574 people have browsed it

注意事项

  • 写v-for必须要写key
  • 写法:写v-for时设置:key=”唯一值”
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>key的作用</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. <ul>
  18. <li v-for="(item,name,index) of list" :key="item.index">
  19. <input type="radio" name="name">
  20. {{item.id}}、{{item.name}}
  21. </li>
  22. </ul>
  23. <input type="submit" value="提交答案" @click.prevent='handle'>
  24. </div>
  25. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  26. <script>
  27. const vm = new Vue({
  28. el: '#app',
  29. data() {
  30. return{ list: [
  31. { id: "a", name: "天蓬老师" },
  32. { id: "b", name: "灭绝老师" },
  33. { id: "c", name: "西门老师" },
  34. { id: "d", name: "郭靖老师" },
  35. ]}
  36. },
  37. methods: {
  38. handle() {
  39. console.log(this.list);
  40. }
  41. },
  42. });
  43. </script>
  44. </body>
  45. </html>

v-if应用于数据渲染

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>key的作用</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. <button @click='flag=!flag' v-text="tips = flag ? `隐藏答案` : `显示答案`"></button>
  18. <span v-if="flag">{{msg}}</span>
  19. </div>
  20. </div>
  21. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  22. <script>
  23. const vm = new Vue({
  24. el: '#app',
  25. data() {
  26. return {
  27. msg: "A",
  28. flag: "ture",
  29. tips: "隐藏当"
  30. }
  31. },
  32. methods: {
  33. handle() {
  34. console.log(this.list);
  35. }
  36. },
  37. });
  38. </script>
  39. </body>
  40. </html>

键盘修饰符

按钮提交enter事件

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>key的作用</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. <button >提交</button>
  18. <input type="text" @keyup.enter="submit($event)" />
  19. <p v-if="list.length > 0">
  20. <span v-for="(item,index) of list" :key="index">
  21. {{item}}
  22. </p>
  23. </span>
  24. </div>
  25. </div>
  26. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  27. <script>
  28. const vm = new Vue({
  29. el: '#app',
  30. data() {
  31. return {
  32. list: []
  33. };
  34. },
  35. methods: {
  36. submit(ev) {
  37. this.list.unshift(ev.target.value);
  38. ev.target.value = null;
  39. }
  40. },
  41. });
  42. </script>
  43. </body>
  44. </html>

vue框架生命周期

vue实例从创建到销毁的全过程
出生到结束的过程(创建、运行、销毁)
生命周期钩子=生命周期函数=生命周期函数
生命周期函数
beforecreate:表示实例被创建出来之前,执行
create:data,methods可以被初始化了
mounted::el可以挂载了,data,methods可以被初始化了
updated:更新完可以输出

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>生命周期</title>
  7. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">{{words}}</div>
  11. <script>
  12. // vue实例从创建到销毁的全过程
  13. // 像一个人从出生到死亡的完整流程
  14. // vue生命周期是函数,为每个周期提供了一个编程接口/钩子,开放出来供用户自定义
  15. const vm = new Vue({
  16. el: document.querySelector("#app"),
  17. data() {
  18. return {
  19. words: "大家晚上吃了吗?",
  20. };
  21. },
  22. // 实例创建之前,data不能用,el不可用
  23. beforeCreate() {
  24. console.log("实例创建前:", this.$el, this.words);
  25. },
  26. // 实例创建之后
  27. created() {
  28. console.log("实例创建完成:", this.$el, this.words);
  29. },
  30. // 挂载完成, 类似于"load"
  31. mounted() {
  32. console.log("挂载创建完成:", this.$el, this.words);
  33. },
  34. // 更新完成
  35. updated() {
  36. console.log("更新完成 data.words =", this.words);
  37. },
  38. });
  39. vm.words = "大家早上好";
  40. </script>
  41. </body>
  42. </html>

计算器属性
使代码更加简洁
基于data中数据做改变
get():取值
set( ):取值后改值
watch():监听器属性
使用时候,写函数名

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>计算属性</title>
  7. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  8. </head>
  9. <body>
  10. <div class="app">
  11. <p>
  12. 数量:
  13. <!-- <input
  14. type="number"
  15. v-model="num"
  16. style="width: 5em"
  17. min="0"
  18. @change="calc"
  19. /> -->
  20. <input type="text" v-model="num" />
  21. </p>
  22. <p>单选题: {{price}} 分</p>
  23. <!-- <p>金额: {{num * price}} 元</p> -->
  24. <!-- <p>金额: {{res}} 元</p> -->
  25. <!-- <p>金额: {{res}} 元</p> -->
  26. <!-- 计算属性,本质上就是原生的访问器属性 -->
  27. <p>单选题总分: {{amount}} 分</p>
  28. </div>
  29. <script>
  30. const vm = new Vue({
  31. el: document.querySelector(".app"),
  32. data() {
  33. return {
  34. num: 0,
  35. price: 2,
  36. res: 0,
  37. };
  38. },
  39. // 计算属性: 最终会和data合并,所以不要和data中已有属性重名
  40. computed: {
  41. // amount() {
  42. // return this.num * this.price;
  43. // },
  44. // 访问器属性有getter/setter
  45. // 计算属性默认是getter
  46. amount: {
  47. get() {
  48. return this.num * this.price;
  49. },
  50. },
  51. },
  52. // methods: {
  53. // calc() {
  54. // this.res = this.num * this.price;
  55. // },
  56. // },
  57. });
  58. vm.amount = 1001;
  59. </script>
  60. </body>
  61. </html>
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>侦听器属性</title>
  7. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  8. </head>
  9. <body>
  10. <div class="app">
  11. <p>
  12. <input
  13. type="number"
  14. v-model="num"
  15. style="width: 5em"
  16. min="0"
  17. :max="max"
  18. />
  19. </p>
  20. <p>单价: {{price}} 元</p>
  21. <p>金额: {{res}} 元</p>
  22. </div>
  23. <script>
  24. const vm = new Vue({
  25. el: document.querySelector(".app"),
  26. data() {
  27. return {
  28. num: 0,
  29. price: 50,
  30. res: 0,
  31. max: 100,
  32. };
  33. },
  34. // 侦听器属性
  35. watch: {
  36. // 侦听的是某一个属性的值的变化,它的属性名与data中要监听的属性同名
  37. num(newValut, oldValue) {
  38. console.log(newValut, oldValue);
  39. // this.res = this.num * this.price;
  40. this.res = newValut * this.price;
  41. // 监听库存
  42. if (newValut >= 20) {
  43. this.max = newValut;
  44. alert("库存不足,请补货");
  45. }
  46. },
  47. },
  48. });
  49. // key: 主要是为了弥补列表渲染时,自动触发diff算法
  50. // v-if, v-else-if, v-show: 条件渲染
  51. // 键盘修饰符
  52. // 计算属性: 相当于原生的访问器属性
  53. // 侦听器属性: 监听数据对象的变化
  54. // 用计算属性完成的功能,侦听器几乎都可以完成
  55. </script>
  56. </body>
  57. </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
Author's latest blog post