ASCII码 ASCII码

vue常用术语、样式与事件绑定和列表渲染

发布于:2022-04-20 14:28:26  栏目:技术文档

1.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. <script src="https://unpkg.com/vue@next"></script>
  8. </head>
  9. <body>
  10. <!-- 挂载点 VUE实例 数据注入 响应式 -->
  11. <!-- 挂载点 -->
  12. <div class="app">
  13. <p>你的名字:{{uesrname}}</p>
  14. </div>
  15. </body>
  16. <script>
  17. //vue实例
  18. const app = Vue.createApp({
  19. data() {
  20. return {
  21. uesrname:'朱老师',
  22. }
  23. },
  24. }).mount('.app');
  25. //数据注入
  26. console.log(app.$data.uesrname);
  27. //响应式
  28. app.uesrname='庆余年';
  29. </script>
  30. </html>

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. <script src="https://unpkg.com/vue@next"></script>
  8. </head>
  9. <body>
  10. <!-- v-html->innerHTML
  11. v-text->textContent
  12. -->
  13. <div class="app">
  14. <p>用户名:{{name}}</p>
  15. <!-- v-text 适用于添加文本 -->
  16. <p>用户名:<span v-text='username'></span></p>
  17. <!-- v-html 用于添加样式 -->
  18. <p>用户名:<span v-html='username'></span></p>
  19. </div>
  20. </body>
  21. <script>
  22. const app = Vue.createApp({
  23. data() {
  24. return {
  25. name:'欧阳',
  26. username:'朱老师'
  27. }
  28. },
  29. }).mount('.app')
  30. app.username='<i style="color:red">毛主席</i>'
  31. </script>
  32. </html>

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. <script src="https://unpkg.com/vue@next"></script>
  8. </head>
  9. <style>
  10. .active {
  11. background: greenyellow;
  12. color: white;
  13. }
  14. </style>
  15. <body>
  16. <!-- v-bind -->
  17. <div class="app">
  18. <!-- 行内样式 -->
  19. <!-- 原声 ES6 -->
  20. <p style="background: red;">我在学习js</p>
  21. <!-- vue 用v-bind -->
  22. <p v-bind:style="style">我在学习vue1</p>
  23. <!-- v-bind 简化 : -->
  24. <p :style="style">我在学习vue2</p>
  25. <!-- class 类样式 -->
  26. <!-- ES6 原生 -->
  27. <p class="active">朱老师你好</p>
  28. <!-- vue 用v-bind 简化 : -->
  29. <p :class="active">朱老师你好</p>
  30. <!-- 样式绑定 -->
  31. <!--
  32. v-on 简化 @
  33. event = $event
  34. -->
  35. <a href="https://www.baidu.com" @click="showUrl($event)">显示网址:</a>
  36. <span class="url">{{url}}</span>
  37. </div>
  38. </body>
  39. <script>
  40. const app = Vue.createApp({
  41. data() {
  42. return {
  43. style: "background:yellow",
  44. active: 'active',
  45. url:''
  46. }
  47. },
  48. //创建一个属性
  49. methods: {
  50. showUrl(ev){
  51. //禁用a标签跳转的默认性行为
  52. ev.preventDefault();
  53. //把当前的a标签的href赋值给url
  54. //this 是指当前vue实例
  55. //ev.target指的是当前a标签
  56. this.url=ev.target.href
  57. }
  58. },
  59. }).mount('.app');
  60. </script>
  61. </html>

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. <script src="https://unpkg.com/vue@next"></script>
  8. </head>
  9. <body>
  10. <div class="app">
  11. <!-- 一个一个获取 -->
  12. <ul>
  13. <li>{{cities[0]}}</li>
  14. <li>{{cities[1]}}</li>
  15. <li>{{cities[2]}}</li>
  16. </ul>
  17. <!-- 使用v-for -> for of for(value of arr) -->
  18. <!-- ::key 必须要添加 diff算法 key 必须保证唯一性 -->
  19. <ul>
  20. <li v-for="(value,index) of cities" ::key="index">{{index}}=>{{value}}</li>
  21. </ul>
  22. <ul>
  23. <!-- :key来干扰diff算法 -->
  24. <li v-for="(value,key) of user" ::key="key">{{key}}=>{{value}}</li>
  25. </ul>
  26. </div>
  27. </body>
  28. <script>
  29. const app = Vue.createApp({
  30. data() {
  31. return {
  32. cities: ['北京', '上海', '广州'],
  33. user:{
  34. name: '张学良',
  35. home: '东北',
  36. email: 'zxl@qq.com'
  37. }
  38. }
  39. },
  40. }).mount('.app');
  41. </script>
  42. </html>
相关推荐
阅读 +