<!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>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app">
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
isShow:true
},
methods:{
show(){
console.log('我要展示我的漂亮');
},
},
beforeCreate() {
this.show()
},
})
</script>
</body>
</html>
解决方法如下:
错误的原因是因为在生命周期函数中beforeCreate还没有进行初始化,改成mounted就对了
<!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>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app">
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
isShow:true
},
methods:{
show(){
console.log('我要展示我的漂亮');
},
},
mounted() {
this.show()
}
})
</script>
</body>
</html>