首页 > 其他 > 详细

vue访问页面时自动触发函数

时间:2020-09-26 18:23:05      阅读:319      评论:0      收藏:0      [点我收藏+]

 

使用 mounted:function () {} 访问页面时自动触发函数内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
    <script src="static/js/vue.js"></script>
    <script src="static/js/axios.min.js"></script>
</head>
<body>
<div id="get_host_data">
    <button @click="GetHostData">获取数据</button>
    <table class="table table-bordered table-hover table-striped">
        <thead>
        <tr>
            <td>ID</td>
            <td>版本包</td>
            <td>md5</td>
        </tr>
        </thead>
        <tbody>
            <tr v-for="(item, index) of host_list" :key="item.pk">
                <td>{{ item.pk }}</td>
                <td>{{ item.fields.package_version }}</td>
                <td>{{ item.fields.md5_number }}</td>
            </tr>
        </tbody>
    </table>
</div>
<script>
    new Vue({
        el:"#get_host_data",
        data: {
            host_list: []
        },
        methods:{
            GetHostData:function(){
                var url = "http://127.0.0.1:8001/super_cmdb/serverhost_list/";
                axios.get(url).then(response => {
                    if (response.data.status==0) {
                        this.host_list = response.data.message;
                        console.log(response.data.message);
                        console.log("获取机器列表成功")
                } else {
                        console.error("获取机器列表失败")
                    }
                })
            }
        },
        mounted:function () {   //自动触发写入的函数
            this.GetHostData();
        }
    })
</script>
</body>
</html>

 

vue访问页面时自动触发函数

原文:https://www.cnblogs.com/chenjw-note/p/13735410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!