0
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../lib/vue.js"></script>
    <script>
        window.onload = function () {
            const app = new Vue({
                el: "#app",
                data: {
                    message: "你好啊",
                    movies: ['星际穿越', '大话西游', '少年派', '盗梦空间']
                }
            })
        }
    </script>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="item in movies">{{item}}</li>
        </ul>
    </div>
</body>

</html>
// 当我在控制台打印vue里面的data时
app.movies
undefined
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../lib/vue.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="item in movies">{{item}}</li>
        </ul>
    </div>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                message: "你好啊",
                movies: ['星际穿越', '大话西游', '少年派', '盗梦空间']
            }
        })
    </script>
</body>
</html>
app.movies
(4) ["星际穿越", "大话西游", "少年派", "盗梦空间", __ob__: Observer]

这俩种加载方式有什么区别吗?

李晗 4
2019-09-11 提问
2 个回答
1

已采纳

第一种方式,你的app定义在匿名函数中,在全局是访问不到的,你控制台打印undefined是很神奇的,这里应该给你提示错误:app is not defined

第二种方式就是全局有个变量app,然后访问,打印出结果,这里的数据是经过vue处理过的(监听)

1

这个就是加载顺序的问题,试一下用window.app试试

撰写答案

推广链接