品牌案例提升(结合vue-resouce)

<!-- 品牌列表改造 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="../vue-2.5.17.js"></script>
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
</head>

<body>
    <div id="app">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Name:
                    <input type="text" v-model="name" class="form-control">
                </label>
                <input type="button" value="添加" @click="add" class="btn btn-primary">
            </div>
        </div>

        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime}}</td>
                    <td><a href="">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                name: "",
                list: [
                    { id: 1, name: "宝马", ctime: new Date() },
                    { id: 2, name: "大众", ctime: new Date() }
                ]
            },
            created() {//当vm实例的data和method初始化完毕后
                this.getAllList();
            },
            methods: {
                add() {
                    /*
                    第一个参数是要请求的url地址
                    第二个参数是要提交给服务器的数据,要以对象的形式提交给服务器{name:this.name}
                    第三个参数是一个配置对象,如果是{emulateJSON:true}就是以表单的形式来提交给服务器
                    application/x-www-form-urlencoded
                    使用.then来设置成功回调的函数,用来拿到成功的结果:result.body
                    */
                    this.$http.post("http://110.80.48.150:8000/%E5%93%81%E7%89%8C%E5%88%97%E8%A1%A8%E6%A1%88%E4%BE%8B/Brand.json",{name:this.name},{emulateJSON:true}).then(function(result){
                        if(result.body.status==0){
                            this.getAllList();
                        }else{
                            alert("添加失败");
                        }
                    })
                },
                getAllList() {
                    //这个链接是本地服务器下的,也就是同目录下的Brand.json文件
                    this.$http.get("http://110.80.48.150:8000/%E5%93%81%E7%89%8C%E5%88%97%E8%A1%A8%E6%A1%88%E4%BE%8B/Brand.json").then(function(result) {
                        var result=result.body;//因为所有的数据都是放在result的body里面的
                        if (result.status == 0) {
                            this.list = result.message;
                        } else {
                            alert("获取数据失败")
                        }
                    })
                }
            }
        });
    </script>
</body>

</html>

 

但是我们每次都要写一遍发送请求的地址辉比较繁琐,所以我们需要存下我们的root地址:

所以

Vue.http.options.root="http://110.80.50.206:8000/%E5%93%81%E7%89%8C%E5%88%97%E8%A1%A8%E6%A1%88%E4%BE%8B"

然后我们下面调用的时候要:

this.$http.get("Brand.json").then(function(result) {
                        var result=result.body;//因为所有的数据都是放在result的body里面的
                        if (result.status == 0) {
                            this.list = result.message;
                        } else {
                            alert("获取数据失败")
                        }
                    })

这里说明了我们需要用相对寻址

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值