与传参## 1. 事件绑定

  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@next"></script>
  9. </head>
  10. <body>
  11. <!-- 复习es6方式 -->
  12. <p>
  13. <a href="https://php.cn" onclick="getUrl()">显示连接地址</a>
  14. <span class="url"></span>
  15. </p>
  16. <script>
  17. function getUrl() {
  18. // 阻止链接默认跳转
  19. event.preventDefault();
  20. // 获取事件的对象a
  21. const link = event.currentTarget;
  22. // 获取a 的兄弟标签,设置标签内容为a 的href属性
  23. link.nextElementSibling.textContent = link.href;
  24. }
  25. </script>
  26. <!-- vue方式 -->
  27. <div class="app">
  28. <p>
  29. <!-- 用v-on事件指令,简写@ -->
  30. <!-- 事件对象event在vue3中为$event -->
  31. <!-- click事件传递$event对象 -->
  32. <a href="https://php.cn" v-on:click="showUrl($event)"
  33. >vue3方式显示连接地址</a
  34. >
  35. <span class="url">{{url}}</span>
  36. </p>
  37. <!-- 事件修饰符:对当前事件行为进行干预 -->
  38. <p>
  39. <!-- 在事件后加.prevent修饰符,禁用默认行为 -->
  40. <a href="https://php.cn" @click.prevent="this.url = $event.target.href"
  41. >vue3事件修饰符方式显示连接地址</a
  42. >
  43. <span class="url">{{url}}</span>
  44. </p>
  45. <p onclick="alert('冒泡来了')">
  46. <!-- 在事件后加.stop修饰符,阻止冒泡 -->
  47. <a
  48. href="https://php.cn"
  49. @click.prevent.stop="this.url = $event.target.href"
  50. >vue3事件修饰符方式阻止冒泡显示连接地址</a
  51. >
  52. <span class="url">{{url}}</span>
  53. </p>
  54. </div>
  55. <script>
  56. const app = Vue.createApp({
  57. data() {
  58. return {
  59. // 当前值为空
  60. url: null,
  61. };
  62. },
  63. // 事件的函数要写在methods属性中
  64. methods: {
  65. showUrl(ev) {
  66. // 传递的参数是事件对象event
  67. // 禁用默认行为
  68. ev.preventDefault();
  69. // this是vue实例,this.url就是占位符{{url}}
  70. // target是事件的目标,这里是a标签,取得href属性
  71. this.url = ev.target.href;
  72. },
  73. },
  74. }).mount(".app");
  75. </script>
  76. </body>
  77. </html>

2 列表渲染

  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@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- 把数组渲染到页面中 -->
  13. <ul>
  14. <!-- 逐一渲染 -->
  15. <li>1->{{city[0]}}</li>
  16. <li>2->{{city[1]}}</li>
  17. <li>3->{{city[2]}}</li>
  18. </ul>
  19. <!-- 用vue指令 -->
  20. <!-- v-for遍历数组 对应js的 for of -->
  21. <!-- ci为值,index为数组索引,city为要遍历的数组 -->
  22. <!-- 一定要加:key,为了借助diff算法,提升遍历效率,key必须选择一个永远不重复的值 -->
  23. <ul>
  24. <li v-for="(ci,index) of city" :key="index">{{index}}->{{ci}}</li>
  25. </ul>
  26. <!-- v-for遍历对象 -->
  27. <!-- v-for="(值,属性名,数字索引) of 对象" -->
  28. <ul>
  29. <li v-for="(value,attrib,index) of users" :key="index">
  30. {{attrib}}=>{{value}}
  31. </li>
  32. </ul>
  33. <!-- v-for遍历数组对象 -->
  34. <ul>
  35. <li v-for="(clas,index) of heros" :key="index">
  36. {{clas.name}}({{clas.add}})
  37. </li>
  38. </ul>
  39. </div>
  40. <script>
  41. const app = Vue.createApp({
  42. data() {
  43. return {
  44. // 数组
  45. city: ["北京", "天津", "上海"],
  46. // 对象
  47. users: {
  48. name: "刀刀",
  49. add: "北湖南路",
  50. },
  51. // 数组对象
  52. heros: [
  53. {
  54. name: "哎哎",
  55. add: "北湖北路",
  56. },
  57. {
  58. name: "万里",
  59. add: "天津路",
  60. },
  61. {
  62. name: "卡卡",
  63. add: "北湖南路",
  64. },
  65. ],
  66. };
  67. },
  68. }).mount(".app");
  69. </script>
  70. </body>
  71. </html>

3. 条件渲染

  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@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- 单条件:if -->
  13. <!-- 控制显示按钮 -->
  14. <!-- 判断show值,动态显示按钮文字 -->
  15. <button @click="show=!show" v-text="show?'隐藏':'显示'"></button>
  16. <!-- 判断show变量是否为真,显示msg -->
  17. <p v-if="show">{{msg}}</p>
  18. <!-- 多条件:if else if else if else -->
  19. <p v-if="point < 2000">{{garde[0]}}</p>
  20. <p v-else-if="point>=2000 && point < 3000">{{garde[1]}}</p>
  21. <p v-else-if="point>=3000 && point < 4000">{{garde[2]}}</p>
  22. <p v-else-if="point>=4000 && point < 5000">{{garde[3]}}</p>
  23. <p v-else="point>=5000">{{garde[4]}}</p>
  24. </div>
  25. <script>
  26. const app = Vue.createApp({
  27. data() {
  28. return {
  29. msg: "这是根据条件控制显示的文字。",
  30. show: "false",
  31. garde: ["勇者", "勇士", "圣斗士", "齐天大圣", "斗战胜佛"],
  32. point: 2000,
  33. };
  34. },
  35. }).mount(".app");
  36. </script>
  37. </body>
  38. </html>

4. 键盘修饰符

  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>todolist: 键盘修饰符</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <label for="msg">留言:</label>
  13. <!-- 键盘修饰符:事件后加.enter就是按下回车键时 -->
  14. <input type="text" id="msg" @keydown.enter="showMsg($event)" />
  15. <ul>
  16. <li v-for="(value,index) of msg" :key="index">{{value}}</li>
  17. </ul>
  18. </div>
  19. <script>
  20. const app = Vue.createApp({
  21. data() {
  22. return {
  23. // 定义变量存贮留言
  24. msg: [],
  25. };
  26. },
  27. methods: {
  28. showMsg(ev) {
  29. // 在数组msg前面放入留言:当前事件对象的值
  30. this.msg.unshift(ev.currentTarget.value);
  31. // 清空输入框
  32. ev.currentTarget.value = null;
  33. },
  34. },
  35. }).mount(".app");
  36. </script>
  37. </body>
  38. </html>

5. 计算属性与侦听器

  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@next"></script>
  9. <style>
  10. .selectAll {
  11. margin-top: 1em;
  12. margin-left: 0.7em;
  13. }
  14. .selectAll *:hover {
  15. cursor: pointer;
  16. }
  17. .shopingList {
  18. padding: 0;
  19. width: 35em;
  20. background-color: lightskyblue;
  21. }
  22. .shopingList > li {
  23. width: 35em;
  24. display: grid;
  25. grid-template-columns: repeat(5, 5fr);
  26. border-bottom: 1px solid rgb(12, 63, 105);
  27. }
  28. .shopingList > li:nth-of-type(1) {
  29. color: #eee;
  30. background-color: rgb(12, 63, 105);
  31. }
  32. h4,
  33. span {
  34. margin: 0.2em 0.6em;
  35. }
  36. h4 {
  37. padding: 0.3em 0;
  38. }
  39. input[type="number"] {
  40. width: 70%;
  41. outline: none;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div class="app">
  47. <ul class="shopingList">
  48. <li>
  49. <h4>品名</h4>
  50. <h4>数量</h4>
  51. <h4>单位</h4>
  52. <h4>单价(元)</h4>
  53. <h4>金额(元)</h4>
  54. </li>
  55. <li>
  56. <span>{{goods[0]}}</span>
  57. <span><input type="number" class="num" v-model="num" /></span>
  58. <span>{{goods[1]}}</span>
  59. <span>{{goods[2]}}</span>
  60. <!-- 计算属性sum -->
  61. <span>{{sum}}</span>
  62. </li>
  63. </ul>
  64. </div>
  65. <script>
  66. const app = Vue.createApp({
  67. data() {
  68. return {
  69. goods: ["V2面膜", "个", 30],
  70. num: 0,
  71. };
  72. },
  73. // 计算属性放在computed属性中
  74. computed: {
  75. sum: {
  76. // 用get方法,就是js访问器属性
  77. get() {
  78. return this.goods[2] * this.num;
  79. },
  80. },
  81. },
  82. // 侦听器属性:侦听计算属性的变化
  83. watch: {
  84. // 计算属性(新值,旧值)
  85. sum(now, old) {
  86. console.log(now, old);
  87. },
  88. },
  89. // mounted方法:当vue实例加载完成后调用方法(类似js的onload)
  90. mounted() {
  91. // 初始化num
  92. this.num = 1;
  93. },
  94. }).mount(".app");
  95. </script>
  96. </body>
  97. </html>

6. 组件

  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@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- vue指令:就是html的自定义属性 -->
  13. <div v-text="'你好'"></div>
  14. <!-- vue组件:就是自定义标签 -->
  15. <!-- 要在vue实例中注册 -->
  16. <!-- 注册命名为驼峰式MyBotton,在html中要改为蛇形my-botton -->
  17. <!-- 定义组件自定义属性username和email,向子组件template中传参 -->
  18. <!-- @evaluate:订阅子组件定义的事件evaluate,接收子组件传来的参数counter,执行eval函数 -->
  19. <My-Botton
  20. username="管理员"
  21. email="admin@163.com"
  22. @evaluate="eval"
  23. ></My-Botton>
  24. </div>
  25. <!-- 在标签template中定义组件内容,根据id绑定vue实例 -->
  26. <template id="counter">
  27. <!-- 点击时变量+1 -->
  28. <button @click="counter++">点赞:{{counter}}</button>
  29. <!-- 使用组件自定义属性, -->
  30. <h4>用户:{{username}}</h4>
  31. <h4>邮箱:{{email}}</h4>
  32. <!-- 与父组件My-Botton通信 -->
  33. <!-- 用$emit发布订阅 -->
  34. <!-- $emit("自定义事件名",向父组件传参数) -->
  35. <!-- 点击按钮时,发布订阅,定义evaluate事件,传递counter参数 -->
  36. <button @click="$emit('evaluate',this.counter)">评价</button>
  37. </template>
  38. <script>
  39. // 创建vue实例,组件要延迟挂载,mount方法分开在注册组件后面写
  40. const app = Vue.createApp({
  41. // 父组件的函数eval要在这里定义
  42. methods: {
  43. eval(count) {
  44. if (count >= 25) {
  45. alert("达标了!");
  46. } else {
  47. alert("未达标!");
  48. }
  49. },
  50. },
  51. });
  52. // 注册组件,用component方法注册MyBotton,
  53. // component("注册组件名称",{组件行为,即方法},data(){return{组件数据}})
  54. app.component("MyBotton", {
  55. // 绑定id为counter的template,这样就可在template标签中定义组件内容
  56. template: "#counter",
  57. // 向子组件传参用props,父组件My-Botton向子组件(template标签中)传username和email
  58. // 组件中的自定义属性用props创建,就可使用
  59. props: ["username", "email"],
  60. data() {
  61. return {
  62. // 定义变量,初化为0
  63. counter: 0,
  64. };
  65. },
  66. });
  67. // 绑定挂载点.app
  68. app.mount(".app");
  69. </script>
  70. </body>
  71. </html>

小结:

  • 父组件向子组件传参:在注册组件方法中,用props:定义父组件的参数,多个参数用数组,就可在子组件中使用参数。
  • 子组件与父组件通信:
    — 在模板中的子组件用$emit发布订阅”事件名”,传递子组件”参数”
    — 在父组件中使用订阅事件 @事件名,定义函数接受参数。

更多相关文章

  1. Android通过PopupMenu定义弹出菜单的位置
  2. android实现双击事件的监听
  3. Android解析微博小尾巴
  4. Android之Widget
  5. android使用Intent操作拨打号码发送短信
  6. RN项目结构、页面组件分析
  7. android WebView 应用内点击超链接不调用系统浏览器
  8. Android触摸事件的传递(八)-View
  9. Android(安卓)widget组件(一):Button、 EditText、TextView

随机推荐

  1. C# Facade外观模式中天河城购物出现的问
  2. C#中异步编程4async与await异步程序开发
  3. .Net中Core使用Socket与树莓派进行通信的
  4. C#中String类型与json之间相互转换的实现
  5. C#中VB.NET给Word文档添加/撤销书签的实
  6. C#编写SqlHelper类的使用详解
  7. 介绍有关C++中继承与多态的基础虚函数类
  8. C# WinForm跨线程访问控件的图文详解
  9. C#中Socket框架的使用教程
  10. C#中关于async与await的使用详解