1.我通过button来调用vue方法但是我的方法放在body和html外部的时候可以正确使用,
但是放在head里面就出错
2.问题代码
var vm=new Vue({
el: '#app',
data: {
showin: true,
message: 'Hello Vue!'
}
})
</script>
3.代码放在head里面出错,但是放在body和html外面不被调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="css.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var vm=new Vue({
el: '#app',
data: {
showin: true,
message: 'Hello Vue!'
}
})
</script>
</head>
<body>
<div id="app">
<div class="app">
<button @click="showin =!showin"> 增加</button>
<a > 删除</a>
<a > 插入</a>
</div>
<div v-show="showin">
<table border="1">
<tr>
<th>id</th>
<th>村民</th>
</tr>
<tr>
<td>1 </td>
<td>张三</td>
</tr>
</table>
</div>
</div>
</body>
</html>
通过问和查,这个问题就是因为html的运行过程就是自上而下的运行,当将vue放在head里面的时候,
先运行了el中的对象去寻找id,这个时候body里面的id还没有加载出来,所以会报错说没有找到id
https://blog.csdn.net/xllily_11/article/details/52312044
要不把 <br> var vm=new Vue({<br> el: '#app',<br> data: {<br> showin: true,<br> message: 'Hello Vue!'<br> }<br> })</p> <pre><code></script>放在body最下面,或者给里面的内容加个window。onload </code></pre>
window.onload=function(){
var vm=new Vue({
el: '#app',
data: {
showin: true,
message: 'Hello Vue!'
}
})
}