说明

image.png

image.png
image.png

命令式编程

一般原生写的JS编程,案例是计数器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <h2 class="counter">0</h2>
  11. <button class="increment">+1</button>
  12. <button class="decrement">-1</button>
  13. <script>
  14. // 1.获取所有的元素
  15. const counterEl = document.querySelector(".counter");
  16. const incrementEl = document.querySelector(".increment");
  17. const decrementEl = document.querySelector(".decrement");
  18. // 2.定义变量
  19. let counter = 100;
  20. counterEl.innerHTML = counter;
  21. // 3.监听按钮的点击
  22. incrementEl.addEventListener("click", () => {
  23. counter += 1;
  24. counterEl.innerHTML = counter;
  25. });
  26. decrementEl.addEventListener("click", () => {
  27. counter -= 1;
  28. counterEl.innerHTML = counter;
  29. });
  30. </script>
  31. </body>
  32. </html>

声明式编程

案例是计数器

Vue 2.x (Options API)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">哈哈哈哈啊</div>
  11. <script src="../js/vue.js"></script>
  12. <script>
  13. Vue.createApp({
  14. template: `
  15. <div>
  16. <h2>{{message}}</h2>
  17. <h2>{{counter}}</h2>
  18. <button @click='increment'>+1</button>
  19. <button @click='decrement'>-1</button>
  20. </div>
  21. `,
  22. data: function() {
  23. return {
  24. message: "Hello World",
  25. counter: 100
  26. }
  27. },
  28. // 定义各种各样的方法
  29. methods: {
  30. increment() {
  31. console.log("点击了+1");
  32. this.counter++;
  33. },
  34. decrement() {
  35. console.log("点击了-1");
  36. this.counter--;
  37. }
  38. }
  39. }).mount('#app');
  40. </script>
  41. </body>
  42. </html>

Vue 3.x (compostition API)

直接通过setup属性,把data、methods 等属性都弄在一起使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">哈哈哈哈啊</div>
  11. <script src="https://unpkg.com/vue@next"></script>
  12. <script>
  13. const {ref} = Vue;
  14. Vue.createApp({
  15. template: `
  16. <div>
  17. <h2>{{message}}</h2>
  18. <h2>{{counter}}</h2>
  19. <button @click='increment'>+1</button>
  20. <button @click='decrement'>-1</button>
  21. </div>
  22. `,
  23. // 定义各种各样的方法
  24. setup() {
  25. const message = "Hello World"
  26. const counter = ref(100)
  27. // 定义各种各样的方法
  28. const increment = () => {
  29. console.log("点击了+1");
  30. counter.value++;
  31. }
  32. const decrement = () => {
  33. console.log("点击了-1");
  34. counter.value--;
  35. }
  36. return {
  37. message,
  38. counter,
  39. increment,
  40. decrement
  41. }
  42. }
  43. }).mount('#app');
  44. </script>
  45. </body>
  46. </html>

优势

声明式 对比 命令式

1、响应式
2、减少代码量和逻辑

==================

MVC

image.png

MVVM

image.png
image.png
image.png

监控Dom的变化
数据绑定,响应式

image.png