HTML文件引入Vue开发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面标题</title>
<script src="https://cdn.suoluomei.com/common/js/jquery-2.1.4.min.js"></script>
<script src="https://cdn.suoluomei.com/common/js2.0/vue/v2.5.16/vue.js"></script>
</head>
<style>
* {
padding: 0;
margin: 0;
}
</style>
<body>
<div id="Vue">
<div>
<p @click="click">{{text}}</p>
<img style="width: 3rem;height: 3rem;" :src="img" alt="">
</div>
<div v-for="item in arr">
<p>{{item.text}}</p>
<img :src="item.img" alt="">
</div>
</div>
</body>
<script>
new Vue({
el: "#Vue",
data: {
text: "hallo,word",
img: "",
arr: []
},
methods: {
click() {
console.log(1111)
}
},
created() {
let that = this
$.ajax({
type: "get", //请求方式
async: false,
url: "__CONTROLLER__/gettesttos",
data: {}, //传值给后台
dataType: "json",
success: function (res) {
console.log(res.info.data)
that.text = res.info.text
that.img = res.info.img
that.arr = res.info.data
},
});
}
})
</script>
<script>
$(document).ready(function () {
});
</script>
</html>
点击事件 @click
<p @click="click">{{text}}</p>
添加文本使用两个大括号{{文本}}
img标签中src前面得加冒号
<img :src="img" alt="">
new Vue({
el: "#Vue",
data: {
text: "hallo,word",
img: "",
arr: []
},
methods: {
click() {
console.log(1111)
}
},
created() {
let that = this
$.ajax({
type: "get", //请求方式
async: false,
url: "__CONTROLLER__/gettesttos",
data: {}, //传值给后台
dataType: "json",
success: function (res) {
console.log(res.info.data)
that.text = res.info.text
that.img = res.info.img
that.arr = res.info.data
},
});
}
})
data中定义需要使用的数据
data: {
text: "hallo,word",
img: "",
arr: []
},
在methods中写入点击事件
methods: {
click() {
console.log(1111)
}
},
请求接口后进行渲染
created() {
let that = this //定义一个that
$.ajax({
type: "get", //请求方式
async: false,
url: "__CONTROLLER__/gettesttos",
data: {}, //传值给后台
dataType: "json",
success: function (res) {
console.log(res.info.data)
that.text = res.info.text
that.img = res.info.img
that.arr = res.info.data
},
});
}
for循环渲染要用v-for
item是自己定义的值
<div v-for="item in arr">
<p>{{item.text}}</p>
<img :src="item.img" alt="">
</div>
arr是需要循环的数组名字