vue 的两种基础结构

162 阅读1分钟

1. 通过 el 作为我们的挂载点

<!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">
        <p>姓名: {{person.name}}</p>
        <p>年龄: {{person.age}}</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                person: {
                    name: 'rayn',
                    age: '18'
                }
            }
        })
    </script>
</body>
</html>

2. 通过render 函数进行渲染

<!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>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script>
        new Vue({
            data: {
                person: {
                    name: 'rayn',
                    age: '18'
                }
            },
            render(h) {
                return h('div', [
                    h('p', '姓名 ' + this.person.name),
                    h('p', '年龄 ' + this.person.age)
                ])
            }
        }).$mount('#app')
    </script>
</body>

</html>