1.基本模板语法-通过html方式展示模板

当template中使用了html语法时,直接使用插值表达式是无法准确展示模板内容的。需要使用v-html指令来绑定模板数据。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo1</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                message: '<strong>hello world</strong>'
            }
        },
        template: '<div v-html="message"></div>',
    });
    const vm = app.mount('#root');
</script>
</html>

绑定属性值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo2</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                disable: false
            }
        },
        template: '<input v-bind:disabled="disable">',
    });
    const vm = app.mount('#root');
</script>
</html>

使用指令v-once后,后续使用vm.$data.message=xxx的赋值形式,不会改变div的展示内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo3</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                message: 'hello world'
            }
        },
        //使用指令v-once后,后续使用vm.$data.message=xxx的赋值形式,不会改变div的展示内容
        template: '<div v-once></div>',
    });
    const vm = app.mount('#root');
</script>
</html>

2.vue语法简写

v-on:click=”xxx”可以简写成@click=”xxx”。v-bind:title=”xxx”可以简写成:title=”xxx”

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo6</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                message: 'hello world',
                show: true
            }
        },
        methods: {
            handleClick() {
                alert('click');
            }
        },
        //v-on:click 可以简写成 @click。v-bind:title 可以简写成 :title
        template: `<div @click="handleClick" :title="message"></div>`,
    });
    const vm = app.mount('#root');
</script>
</html>

3.阻止默认行为-网页跳转

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo7</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                message: 'hello world',
                show: true,
                name: 'title',
                event: 'click'
            }
        },
        methods: {
            handleClick(e) {
                e.preventDefault();
            }
        },
        template: `<form action="https://www.baidu.com" @click='handleClick'>
                    <button type="submit">提交</button>
                    </form>`,
    });
    const vm = app.mount('#root');
</script>
</html>

或者可以使用如下简写的方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo7</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                message: 'hello world',
                show: true,
                name: 'title',
                event: 'click'
            }
        },
        methods: {
            handleClick() {
                alert("click");
            }
        },
        template: `<form action="https://www.baidu.com" @click.prevent='handleClick'>
                    <button type="submit">提交</button>
                    </form>`,
    });
    const vm = app.mount('#root');
</script>
</html>

4.计算属性和侦听器

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>demo9</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
  <div id="root"></div>
</body>
<script>
  // data & methods & computed & watcher
  // computed 和 method 都能实现的一个功能,建议使用 computed,因为有缓存
  // computed 和 watcher 都能实现的功能,建议使用 computed 因为更加简洁
  const app = Vue.createApp({
    data() {
      return {
        message: "hello world",
        count: 2,
        price: 5,
        newTotal: 10,
      }
    },
    watch: {
      // price 发生变化时,函数会执行
      price(current, prev) {
        this.newTotal = current * this.count;
      }
    },
    computed: {
      // 当计算属性依赖的内容发生变更时,才会重新执行计算
      total() {
        return Date.now() + this.count;
        // return this.count * this.price
      }
    },
    methods: {
      formatString(string) {
        return string.toUpperCase();
      },
      // 只要页面重新渲染,才会重新计算
      getTotal() {
        return Date.now();
        // return this.count * this.price;
      },
    },
    template: `
     <div>   </div>
    `
  });
  const vm = app.mount('#root');
</script>
</html>

5.vue样式写法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>demo10</title>
  <style>
    .red {
      color: red;
    }
    .green {
      color: green;
    }
  </style>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
  <div id="root"></div>
</body>
<script>
  const app = Vue.createApp({
    data() {
      return {
        classString: 'red',
        classObject: { red: false, green: true },
        classArray: ['red', 'green', {blue: true}],
        styleString: 'color: yellow;background: orange',
        styleObject: {
          color: 'orange',
          background: 'yellow'
        }
      }
    },
    /*v-bind:class  简写  :class*/
    /*$attrs.class 从父组件class属性上取值*/
    template: `
        <div :class="classString">
            Hello World1
        </div>
        <div :class="classObject">
            Hello World2
        </div>
        <div :class="classArray">
            Hello World3
        </div>
        <div :class="classArray">
            Hello World4
            <demo class="red"/>
        </div>
        <div :style="styleObject">
            Hello World5
        </div>
    `
  });

  app.component('demo', {
    template: `
      <div :class="$attrs.class">one</div>
      <div :class="$attrs.class">two</div>
    `
  })

  const vm = app.mount('#root');
</script>
</html>

6.条件渲染

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>demo11</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
  <div id="root"></div>
</body>
<script>
  const app = Vue.createApp({
    data() {
      return {
        show: false,
        conditionOne: false,
        conditionTwo: true
      }
    },
    /*v-if 控制是否显示是直接从dom上增加或者移除. v-show 控制是否显示是通过display属性控制是否显示*/
    template: `
      <div v-if="show">Hello World</div>

      <div v-if="conditionOne">if</div>
      <div v-else-if="conditionTwo">elseif</div>
      <div v-else>else</div>

      <div v-show="show">Bye World</div>
    `
  });

  const vm = app.mount('#root');
</script>
</html>
原文链接: https://marshucheng1.github.io/2022/04/13/vue-4/