• 667 Vue模板语法:Mustache,基本指令


    methods方法绑定this


    问题一:不能使用箭头函数?

    https://mp.weixin.qq.com/s/hYm0JgBI25grNG_2sCRlTA;


    问题二:this到底指向什么?

    事实上Vue的源码当中就是对methods中的所有函数进行了遍历,并且通过bind绑定了this:


    VSCode代码片段

    https://snippet-generator.app/


    代码片段过程


    模板语法


    Mustache双大括号语法


    01_Mustache语法.html

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
      <div id="app"></div>
    
      <template id="my-app">
        <!-- 1.mustache的基本使用 -->
        <h2>{{message}} - {{message}}</h2>
        <!-- 2.是一个表达式 -->
        <h2>{{counter * 10}}</h2>
        <h2>{{ message.split(" ").reverse().join(" ") }}</h2>
        <!-- 3.也可以调用函数 -->
        <!-- 可以使用computed(计算属性) -->
        <h2>{{getReverseMessage()}}</h2>
        <!-- 4.三元运算符 -->
        <h2>{{ isShow ? "哈哈哈": "" }}</h2>
        <button @click="toggle">切换</button>
    
        <!-- 错误用法 -->
        <!-- var name = "abc" -> 赋值语句 -->
        <!-- <h2>{{var name = "abc"}}</h2>
        <h2>{{ if(isShow) {  return "哈哈哈" } }}</h2> -->
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        const App = {
          template: '#my-app',
          data() {
            return {
              message: "Hello World",
              counter: 100,
              isShow: true
            }
          },
          methods: {
            getReverseMessage() {
              return this.message.split(" ").reverse().join(" ");
            },
            toggle() {
              this.isShow = !this.isShow;
            }
          }
        }
    
        Vue.createApp(App).mount('#app');
      </script>
    </body>
    
    </html>
    

    v-once指令


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <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>
    </html>
    

    v-text指令


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <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>
    </html>
    

    v-html


    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <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>
    
    </html>
    

    v-pre


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <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>
    </html>
    

    v-cloak


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <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>
    </html>
    

  • 相关阅读:
    今天开通我的博客
    在ArcGIS中,利用“行政单元面积权重法”实现人口数据格网化
    ArcGIS 下的水文分析
    常用计数器的verilog实现(binary、gray、onehot、LFSR、环形、扭环形)
    简单组合逻辑电路的verilog实现(包括三态门、38译码器、83优先编码器、8bit奇偶校验器)
    乘法器的verilog实现(并行、移位相加、查找表)
    简单时序逻辑电路的verilog实现,包括D触发器、JK触发器、锁存器、寄存器、
    简单ALU(算术逻辑单元)的verilog实现
    ubuntu下安装virtualbox 错误及解决办法
    C++单例模式对象的控制释放分析
  • 原文地址:https://www.cnblogs.com/jianjie/p/14771853.html
Copyright © 2020-2023  润新知