vue3基础-模版语法

2022-04-19  本文已影响0人  GIDK

1、VSCode代码片段:https://snippet-generator.app/
2、mustache基本使用:

image.png
3、v-once用于指定元素或者组件只渲染一次:
<body>
  <div id="app"></div>
  <template id="my-app">
    <h2>{{counter}}</h2>
    <div v-once>
      <h2>{{counter}}</h2>
      <h2>{{message}}</h2>
    </div>
    <button @click="increment">+1</button>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          counter: 100,
          message: "abc"
        }
      },
      methods: {
        increment() {
          this.counter++;
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

4、v-text指令,用于更新元素的 textContent:

<body>
  <div id="app"></div>
  <template id="my-app">
    <h2 v-text="message"></h2>
    <h2>{{message}}</h2>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          message: "Hello World"
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

5、v-html,默认情况下,如果我们展示的内容本身是 html 的,那么vue并不会对其进行特殊的解析:

<body>
  <div id="app"></div>
  <template id="my-app">
    <div>{{msg}}</div>
    <div v-html="msg"></div>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          msg: '<span style="color:red; background: blue;">哈哈哈</span>'
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

6、v-pre用于跳过元素和它的子元素的编译过程,显示原始的Mustache标签

<body>
  <div id="app"></div>
  <template id="my-app">
    <h2 v-pre>{{message}}</h2>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          message: "Hello World"
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

7、v-cloak这个指令保持在元素上直到关联组件实例结束编译
和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Mustache 标签直到组件实例准备完毕。<div> 不会显示,直到编译结束。

<style>
    [v-cloak] {
      display: none;
    }
  </style>
</head>
<body>
  <div id="app"></div>
  <template id="my-app">
    <h2 v-cloak>{{message}}</h2>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          message: "Hello World"
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

8、v-bind的绑定属性 缩写::

<body>
  <div id="app"></div>
  <!-- vue2 template模板中只能有一个根元素 -->
  <!-- vue3 是允许template中有多个根元素 -->
  <template id="my-app">
    <!-- 1.v-bind的基本使用 -->
    <img v-bind:src="imgUrl" alt="">
    <a v-bind:href="link">百度一下</a>
    <!-- 2.v-bind提供一个语法糖 : -->
    <img :src="imgUrl" alt="">
    <img src="imgUrl" alt="">
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          imgUrl: "https://avatars.githubusercontent.com/u/10335230?s=60&v=4",
          link: "https://www.baidu.com"
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

9、绑定class介绍,绑定class有两种方式:对象语法、数组语法

<style>
    .active {
      color: red;
    }
  </style>
</head>
<body>
  <div id="app"></div>
  <template id="my-app">
    <div :class="className">哈哈哈哈</div>
    <!-- 对象语法: {'active': boolean} -->
    <div :class="{'active': isActive}">呵呵呵呵</div>
    <button @click="toggle">切换</button>
    <!-- 也可以有多个键值对 -->
    <div :class="{active: isActive, title: true}">呵呵呵呵</div>
    <!-- 默认的class和动态的class结合 -->
    <div class="abc cba" :class="{active: isActive, title: true}">
      呵呵呵呵
    </div>
    <!-- 将对象放到一个单独的属性中 -->
    <div class="abc cba" :class="classObj">呵呵呵呵</div>
    <!-- 将返回的对象放到一个methods(computed)方法中 -->
    <div class="abc cba" :class="getClassObj()">呵呵呵呵</div>      
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: "#my-app",
      data() {
        return {
          className: "why",
          isActive: true,
          title: "abc",
          classObj: { 
            active: true, 
            title: true 
          },
        };
      },
      methods: {
        toggle() {
          this.isActive = !this.isActive;
        },
        getClassObj() {
          return { 
            active: true, 
            title: true 
          }
        }
      },
    };
    Vue.createApp(App).mount("#app");
  </script>
</body>
<body>
//数组语法:
<div id="app"></div>
<template id="my-app">
  <div :class="['abc', title]">哈哈哈哈</div>
  <div :class="['abc', title, isActive ? 'active': '']">哈哈哈哈</div>
  <div :class="['abc', title, {active: isActive}]">哈哈哈哈</div>
</template>
<script src="../js/vue.js"></script>
<script>
  const App = {
    template: '#my-app',
    data() {
      return {
        message: "Hello World",
        title: "cba",
        isActive: true
      }
    }
  }
  Vue.createApp(App).mount('#app');
</script>
</body>

10、绑定style介绍,来绑定一些CSS内联样式,对象语法、数组语法

  <!-- 对象语法 -->
<body>
  
  <div id="app"></div>

  <template id="my-app">
    <!-- :style="{cssPropertyName: cssPropertyValue}" -->
    <div :style="{color: finalColor, 'font-size': '30px'}">哈哈哈哈</div>
    <div :style="{color: finalColor, fontSize: '30px'}">哈哈哈哈</div>
    <div :style="{color: finalColor, fontSize: finalFontSize + 'px'}">哈哈哈哈</div>

    <!-- 绑定一个data中的属性值, 并且是一个对象 -->
    <div :style="finalStyleObj">呵呵呵呵</div>
    <!-- 调用一个方法 -->
    <div :style="getFinalStyleObj()">呵呵呵呵</div>
  </template>

  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          message: "Hello World",
          finalColor: 'red',
          finalFontSize: 50,
          finalStyleObj: {
            'font-size': '50px',
            fontWeight: 700,
            backgroundColor: 'red'
          }
        }
      },
      methods: {
        getFinalStyleObj() {
          return {
            'font-size': '50px',
            fontWeight: 700,
            backgroundColor: 'red'
          }
        }
      }
    }

    Vue.createApp(App).mount('#app');
  </script>
</body>
  <!-- 数组语法 -->
<body>
  
  <div id="app"></div>

  <template id="my-app">
    <div :style="[style1Obj, style2Obj]">哈哈哈</div>
    <img :src="" alt="">
    <a :href=""></a>
    <div :class></div>
  </template>

  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          message: "Hello World",
          style1Obj: {
            color: 'red',
            fontSize: '30px'
          },
          style2Obj: {
            textDecoration: "underline"
          }
        }
      }
    }

    Vue.createApp(App).mount('#app');
  </script>
</body>

11、动态绑定属性,,在某些情况下,我们属性的名称可能也不是固定的,
如果属性名称不是固定的,我们可以使用 :[属性名]=“值” 的格式来定义;

<body>
  <div id="app"></div>
  <template id="my-app">
    <div :[name]="value">哈哈哈</div>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          name: "cba",
          value: "kobe"
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

12、绑定一个对象,将一个对象的所有属性,绑定到元素上的所有属性

<body>
  <div id="app"></div>
  <template id="my-app">
    <div v-bind="info">哈哈哈哈</div>
    <div :="info">哈哈哈哈</div>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          info: {
            name: "why",
            age: 18,
            height: 1.88
          }
        }
      }
    }
    Vue.createApp(App).mount('#app');
  </script>
</body>

13、v-on的用法 ,1、 缩写:2 、 预期:Function | Inline Statement | Object
3、 参数:event
4、 修饰符:
.stop - 调用 event.stopPropagation()。
.prevent - 调用 event.preventDefault()。
.capture - 添加事件侦听器时使用 capture 模式。
.self - 只当事件是从侦听器绑定的元素本身触发时才触发回调。
.{keyAlias} - 仅当事件是从特定键触发时才触发回调。
.once - 只触发一次回调。
.left - 只当点击鼠标左键时触发。
.right - 只当点击鼠标右键时触发。
.middle - 只当点击鼠标中键时触发。
.passive - { passive: true } 模式添加侦听器

p 用法:绑定事件监听 image.png 14、v-on参数传递 image.png 15、v-on的修饰符 image.png 16、条件渲染 image.png 17、template元素 image.png image.png
18、v-show image.png 19、v-show和v-if的区别 image.png 20、v-for基本使用 image.png image.png 21、数组更新检测
push()、pop()、shift()、unshift()、splice()、sort()、reverse()、替换数组的方法上面的方法会直接修改原来的数组,但是某些方法不会替换原来的数组,而是会生成新的数组,比如 filter()、concat() 和 slice()。
上一篇 下一篇

猜你喜欢

热点阅读