vuejs与外界通信

源代码:

其中在vue项目中用npm安装jquery,vue-resource插件的的命令行:

npm install jquery

npm install vue-resource

import VueResource from 'vue-resource'
    import Vue from 'vue'
    import $ from 'jquery'
    import A from './a.js'
    Vue.use(VueResource);
    export default {
        name: 'hello',
        props: ['src'],
        data: function () {
            return {
                srcD: ''
            }
        },
        mounted: function () {
            this.srcD = this.src
        },
        watch: {
            srcD: function (newV, oldV) {
                console.info('src:' + this.srcD)
                var options = {
                    method: 'GET',
                    url: 'http://www.baidu.com'
                }
                //jquery
                $.ajax({
                    url:  'http://127.0.0.1:8080',
                    type: 'get',
                    dataType: 'html',
                    success: function (data){
                        console.log('jquery ajax 的請求')
                        console.log(data)
                    },
                    error: function(data) {
                        console.info(data)
                    }
                })
                //这个是对的
                var resource = this.$resource("http://127.0.0.1:8080")
                resource.get().then(function (response) {
                    console.log('this.$resource 的請求')
                    console.log(response.body)
                }).catch(function (response) {
                    console.log(response)
                })
                //这个也是对的
                this.$http.get("http://127.0.0.1:8080").then(function (res) {
                            // 处理成功的结果
                            console.log('this.$http 的請求')
                            console.info(res.body)
                            $(".contentCenter").html(res.body)
                        }, function (res) {
                            console.info(res)
                        }
                );
            }
        }
    }


结果:

src:http://www.baidu.com
jquery ajax 的請求
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>server</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  <script type="text/javascript" src="/app.js"></script></body>
</html>

this.$resource 的請求
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>server</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  <script type="text/javascript" src="/app.js"></script></body>
</html>

this.$http 的請求
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>server</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  <script type="text/javascript" src="/app.js"></script></body>
</html>

这个界面是Hello.vue中的Script内容


vue-resource详解文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值