HTML文件引入Vue开发

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是需要循环的数组名字
arr是需要循环的数组名字

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值