Blogger Information
Blog 33
fans 0
comment 0
visits 18737
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue常用术语、样式与事件绑定和列表渲染
李玉峰
Original
379 people have browsed it

1.vue常用术语

实例演示:挂载点,vue实例,数据注入,响应式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>挂载点,vue实例,数据注入,响应式</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <!-- 1.挂载点:当前vue实例的作用域 -->
  12. <div class="app">
  13. <p>用户名:{{ username }}</p>
  14. </div>
  15. <script>
  16. // 2.vue实例
  17. const app = Vue.createApp(
  18. // vue实例配置对象,要写到一对{}中
  19. {
  20. // vue中的变量/数据全部要写到一个data()
  21. data() {
  22. // 返回所有变量
  23. return {
  24. username: "李老师",
  25. };
  26. },
  27. }
  28. // app.mount(".app");
  29. ).mount(".app");
  30. // console.log(app);
  31. // 绑定:将vue实例与挂载点进行绑定
  32. // app.mount(document.querySelector(".app"));
  33. // document.querySelector(".app") => '.app'
  34. // 3.数据注入:本质就是访问器属性
  35. console.log(app.username);
  36. // 完整注入
  37. console.log(app.$data.username);
  38. // 访问器属性
  39. const obj = {
  40. $data: {
  41. email: "admin@qq.com",
  42. },
  43. // 访问器属性
  44. get email() {
  45. return this.$data.email;
  46. },
  47. };
  48. console.log(obj.email);
  49. // 4.响应式,vue实例中的数据,实时响应外部对数据的更新
  50. // 外部的”张老师“ 改变内部的 ”李老师“
  51. app.username = "张老师";
  52. </script>
  53. </body>
  54. </html>

2.v-html和v-text内容填充

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>v-html和v-text内容填充</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <p>用户名:{{username}}</p>
  13. <p>用户名:<span v-text="username"></span></p>
  14. <p>用户名:<span v-html="username"></span></p>
  15. </div>
  16. <script>
  17. // innerHTML-> vue:v-html
  18. // textContent -> vue:v-text
  19. const app = Vue.createApp({
  20. data() {
  21. return {
  22. username: "李老师",
  23. };
  24. },
  25. }).mount(".app");
  26. app.username = "<i style='color:red'>王老师</i>";
  27. </script>
  28. </body>
  29. </html>

3.样式与事件绑定

行内样式和类样式绑定

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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@next"></script>
  9. <style>
  10. .active {
  11. color: green;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="app">
  17. <!-- 行内样式 -->
  18. <p style="color: red">传统方式样式添加</p>
  19. <p v-bind:style="style">样式绑定</p>
  20. <!-- 语法糖简化:v-bind: =>:-> -->
  21. <p :style="style">语法糖简化方式</p>
  22. <!-- 多个变量改变 -->
  23. <p :style="{color:mycolor,background:mybackground}">语法糖多个变量用对象方式</p>
  24. <!-- class:类样式 -->
  25. <p class="active">class:类样式原生语法演示</p>
  26. <p :class="active">class:类样式vue演示</p>
  27. </div>
  28. <script>
  29. const app = Vue.createApp({
  30. data() {
  31. return {
  32. style: "color:cyan",
  33. mycolor: "blue",
  34. mybackground: "yellow",
  35. active: "active",
  36. };
  37. },
  38. }).mount(".app");
  39. </script>
  40. </body>
  41. </html>

4.列表渲染

显示效果

HTML代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <p>数组渲染</p>
  13. <ul>
  14. <li>{{cities[0]}}</li>
  15. <li>{{cities[1]}}</li>
  16. <li>{{cities[2]}}</li>
  17. </ul>
  18. <ul>
  19. <!-- v-for -> for of -->
  20. <!-- <li v-for="city of cities">{{city}}</li> -->
  21. <!-- :key 必须要添加,必须保证唯一性,diff算法:号称vue精华-->
  22. <li v-for="(city,index) of cities" :key="index">{{index}}=>{{city}}</li>
  23. </ul>
  24. <hr />
  25. <p>对象渲染</p>
  26. <ul>
  27. <li v-for="item of user">{{item}}</li>
  28. <li v-for="(item,key) of user" :key="key">{{key}}=>{{item}}</li>
  29. </ul>
  30. </div>
  31. <script>
  32. const app = Vue.createApp({
  33. data() {
  34. return {
  35. //array
  36. cities: ["兰州", "天水", "白银"],
  37. //obj
  38. user: {
  39. name: "李老师",
  40. age: "29",
  41. },
  42. };
  43. },
  44. }).mount(".app");
  45. </script>
  46. </body>
  47. </html>
Correcting teacher:PHPzPHPz

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