• vue模板语法下集


    目录

    一、事件处理器

     二、自定义组件

    三、组件通信

    四、表单案例

    一、事件处理器

    事件修饰符

      Vue通过由点(.)表示的指令后缀来调用修饰符

      .stop

      .prevent

      .capture

      .self

      .once

    按键修饰符

      Vue允许为v-on在监听键盘事件时添加按键修饰符

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. <style>
    8. .red {
    9. width: 400px;
    10. height: 400px;
    11. background-color: red;
    12. }
    13. .yellow {
    14. width: 300px;
    15. height: 300px;
    16. background-color: yellow;
    17. }
    18. .blue {
    19. width: 200px;
    20. height: 200px;
    21. background-color: blue;
    22. }
    23. .green {
    24. width: 100px;
    25. height: 100px;
    26. background-color: green;
    27. }
    28. style>
    29. head>
    30. <body>
    31. <div id="app">
    32. <p冒泡事件>
    33. p>
    34. <div class="red" @click="red">
    35. <div class="yellow" @click.stop="yellow">
    36. <div class="blue" @click="blue">
    37. <div class="green" @click.stop="green">div>
    38. div>
    39. div>
    40. div>
    41. <p>只能发送一次请求p>
    42. <button @click.once="once">点我试试button>
    43. <p>案件修饰符p>
    44. <input @keyup.enter="enter"/>
    45. div>
    46. body>
    47. <script>
    48. new Vue({
    49. el: "#app",
    50. data() {
    51. return {
    52. f200: 'f200'
    53. };
    54. },
    55. methods: {
    56. red() {
    57. alert("red");
    58. },
    59. yellow() {
    60. alert("yellow");
    61. },
    62. blue() {
    63. alert("blue");
    64. },
    65. green() {
    66. alert("green");
    67. },
    68. once(){
    69. alert("试试就试试");
    70. },
    71. enter(){
    72. alert("hello,vue!");
    73. }
    74. }
    75. })
    76. script>
    77. html>

     加.stop之前 点击yellow区域还有red弹出

     

     加.stop之后就只有选择的区域了

     

     再点击就无效了

     案件修饰符

     二、自定义组件

    组件(Component)是Vue最强大的功能之一

          组件可以扩展HTML元素,封装可重用的代码

          组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <mybutton m="cdl">mybutton>
    11. <mybutton m="小黄人">mybutton>
    12. div>
    13. body>
    14. <script>
    15. Vue.component('mybutton',{
    16. //props是定义组件中的变量的
    17. props:['m'],
    18. //template代表了自定义组件在页面上显示的内容
    19. template:'',
    20. data:function(){
    21. return {
    22. n:1
    23. }
    24. },
    25. methods:{
    26. point(){
    27. this.n++;
    28. }
    29. }
    30. });
    31. //绑定边界
    32. new Vue({
    33. el:"#app",
    34. data() {
    35. return {
    36. };
    37. }
    38. })
    39. script>
    40. html>

     起始次数

     

     组件点击一次加一次次数

    三、组件通信

    自定义事件

       监听事件:$on(eventName)

       触发事件:$emit(eventName)

    Vue自定义事件是为组件间通信设计

            父Vue实例->Vue实例,通过prop传递数据

            子Vue实例->父Vue实例,通过事件传递数据         

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <ul>
    11. <li>
    12. <h3>{{ts}}h3>
    13. <p>组件通信p>
    14. <mybutton m="cdl" v-on:three-click="xxx">mybutton>
    15. li>
    16. ul>
    17. div>
    18. body>
    19. <script>
    20. Vue.component('mybutton',{
    21. //props是定义组件中的变量的
    22. props:['m'],
    23. //template代表了自定义组件在页面上显示的内容
    24. template:'',
    25. data:function(){
    26. return {
    27. n:1
    28. }
    29. },
    30. methods:{
    31. point(){
    32. console.log('point方法被调用');
    33. this.n++;
    34. if(this.n % 3 ==0){//父-->子
    35. //触发自定义组件的事件,这里可以传递任意个参数
    36. //但是触发的事件绑定的函数要与参数个数一致
    37. this.$emit('three-click',this.n,'http://www.baidu.com','cdl');
    38. }
    39. }
    40. }
    41. });
    42. //绑定边界
    43. new Vue({
    44. el:"#app",
    45. data() {
    46. return {
    47. //ts:new Data().getTime()
    48. };
    49. },
    50. methods:{
    51. xxx:function(a,b,c){
    52. console.log("自定义事件绑定的函数被执行...");//子-->父
    53. console.log(a);
    54. console.log(b);
    55. console.log(c);
    56. }
    57. }
    58. })
    59. script>
    60. html>

     

    四、表单案例

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>表单案例title>
    6. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
    7. head>
    8. <body>
    9. <div id="app">
    10. <form>
    11. <ul>
    12. <li>
    13. <p>表单p>
    14. <label>姓名:label><input v-model="uname" /><br />
    15. <label>密码:label><input v-model="upwd" type="password" /><br />
    16. <label>年龄:label><input v-model.number="age" /><br />
    17. <label>性别:label>
    18. <input type="radio" v-model="sex" name="sex" value="1" />
    19. <input type="radio" v-model="sex" name="sex" value="0" /><br />
    20. <label>爱好:label>
    21. <div v-for="h in hobby">
    22. <input type="checkbox" v-model="hobbies" v-bind:value="h.id" />{{h.name}}
    23. div>
    24. <label>类别:label>
    25. <select v-model="type">
    26. <option value="-1">===请选择====option>
    27. <option v-for="t in types" v-bind:value="t.id">{{t.name}}option>
    28. select><br />
    29. <label>备注:label>
    30. <textarea v-bind:value="mark">textarea><br />
    31. 确认<input type="checkbox" v-model="flag" />
    32. <input type="submit" v-bind:disable="show" v-on:click="doSubmit" />
    33. li>
    34. ul>
    35. form>
    36. div>
    37. body>
    38. <script>
    39. new Vue({
    40. el: '#app',
    41. data() {
    42. return {
    43. uname: null,
    44. upwd: null,
    45. age: 10,
    46. sex: 1,
    47. hobby: [{
    48. id: 1,
    49. name: '篮球'
    50. }, {
    51. id: 2,
    52. name: '足球'
    53. }, {
    54. id: 3,
    55. name: '象棋'
    56. }],
    57. hobbies: [],
    58. types: [{
    59. id: 1,
    60. name: 'A'
    61. }, {
    62. id: 2,
    63. name: 'B'
    64. }, {
    65. id: 3,
    66. name: 'C'
    67. }],
    68. type: null,
    69. mark: '学生备注',
    70. flag: false
    71. }
    72. },
    73. computed: {
    74. show: function() {
    75. return !this.flag;
    76. }
    77. },
    78. methods: {
    79. doSubmit: function() {
    80. console.log('doSubmit')
    81. var obj = {
    82. uname: this.uname,
    83. upwd: this.upwd,
    84. age: this.age + 10,
    85. sex: this.sex,
    86. hobbies: this.hobbies,
    87. type: this.type,
    88. mark: this.mark,
    89. }
    90. console.log(obj);
    91. }
    92. }
    93. })
    94. script>
    95. html>

     

  • 相关阅读:
    人工智能在软件测试领域的应用研究
    ubuntu编译和链接特定版本的opencv和boost
    Dubbo的使用
    Jmeter接口测试教程
    来用Vite+React快速开发浏览器插件
    既然有了量化交易,技术分析还有存在的必要么?有专门收割自动交易系统的策略吗?
    python 中的迭代器和生成器简单介绍
    玩转TypeScript之玩转数组(完整版)
    算法2:链表的逆转
    K8s Kubelet 垃圾回收机制
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126694638