Blogger Information
Blog 94
fans 0
comment 0
visits 91867
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Vue】Vue常用术语和指令
可乐随笔
Original
1444 people have browsed it

Vue常用术语和指令

1. Vue常用术语

  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>常用术语</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <!-- 1.挂载点(盒子/容器):应用实例作用域 -->
  12. <div class="app">
  13. <p>用户名:{{username}}</p>
  14. </div>
  15. <script>
  16. // 2. vue根组件应用实例
  17. const app = Vue.createApp({
  18. //根组件配置项
  19. data: function () {
  20. return {
  21. // 将组件中要用到的数据全部声明在这个返回的对象中
  22. username: '老马',
  23. };
  24. },
  25. });
  26. // 3.根组件实例与挂载点进行绑定
  27. const vm = app.mount('.app');
  28. // console.log(vm);
  29. // 4.数据注入:data声明的数据会自动注入当前应用实例中
  30. // console.log(vm.$data.username);
  31. console.log(vm.username);
  32. // 5.响应式
  33. vm.username = '老李';
  34. // 1. 我没有进行dom操作,选择元素,更新内容
  35. // 2. 没有进行事件绑定,没有监听,这些都是vue搞定的
  36. </script>
  37. </body>
  38. </html>

2. Vue 简单指令

总结:v-site:v开头的自定义属性,类似 data-key 自定义属性。Vue可以解析。

  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>简单指令</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <!-- vue指令:`v-` 前缀,本质是来说,就是自定义标签属性 -->
  12. <!-- <div class="app" data-key="1" v-site="www.php.cn"></div> -->
  13. <!-- class:预定义
  14. data-key;v-site:自定义属性 -->
  15. <!-- 挂载点 -->
  16. <div class="app">
  17. <p>用户名:{{username}}</p>
  18. <!-- ele.textContent / ele.innerText -->
  19. <p v-text="hello"></p>
  20. <!-- ele.innerHtml -->
  21. <p v-html="eat"></p>
  22. <!-- 只渲染一次 -->
  23. <p v-once="eat"></p>
  24. </div>
  25. <script>
  26. // console.log(document.querySelector('.app').getAttribute('v-site'));
  27. const app = Vue.createApp({
  28. data() {
  29. return {
  30. username:'老马',
  31. hello:'早上好!',
  32. eat:'<em style="color:red;">您吃了吗?</em>'
  33. }
  34. },
  35. });
  36. app.mount('.app');
  37. </script>
  38. </body>
  39. </html>

3. Vue样式绑定

v-bind:高频指令,可以简化,冒号:
v-bind:通常定义的是HTML预定义的属性
如:v-bind:style 也可以简写::style

  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>样式绑定</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. <style>
  10. .active {
  11. color: red;
  12. }
  13. .bgc {
  14. background-color: lightblue;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="app">
  20. <!-- 1. 行内样式绑定,大多使用对象 -->
  21. <p style="color: red; background-color: yellow">hello world</p>
  22. <!-- vue来接管style属性 -->
  23. <!-- style,v-bind:style / style是预定义属性,v-bind:style是vue自定义属性-->
  24. <p v-bind:style="{color:myColor,backgroundColor:bgc}">hello world</p>
  25. <!-- 2. 类样式:class,大多使用数组 -->
  26. <!-- classList -->
  27. <p class="active bgc">大家累了吧</p>
  28. <!-- vue接管类样式 -->
  29. <p v-bind:class="[jh,bj]">大家累了吧</p>
  30. <!-- 对象,布尔 ,v-bind 可以省略 -->
  31. <p :class="{active:isActive,bgc:isBgc}">大家累了吧</p>
  32. <!-- v-bind:高频指令,可以简化,冒号: -->
  33. <!-- v-bind:通常定义的是HTML预定义的属性 -->
  34. </div>
  35. <script>
  36. const rootConfig = {
  37. data() {
  38. return {
  39. myColor: 'red',
  40. bgc: 'yellow',
  41. jh: 'active',
  42. bj: 'bgc',
  43. isActive: true,
  44. isBgc:false,
  45. };
  46. },
  47. };
  48. const app = Vue.createApp(rootConfig);
  49. app.mount('.app');
  50. </script>
  51. </body>
  52. </html>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!