vuejs与外界通信

来源:互联网 发布:如何卸载linux系统 编辑:程序博客网 时间:2024/05/16 02:21

源代码:

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

npm install jquery

npm install vue-resource

[javascript] view plain copy
  1. import VueResource from 'vue-resource'  
  2.     import Vue from 'vue'  
  3.     import $ from 'jquery'  
  4.     import A from './a.js'  
  5.     Vue.use(VueResource);  
  6.     export default {  
  7.         name: 'hello',  
  8.         props: ['src'],  
  9.         data: function () {  
  10.             return {  
  11.                 srcD: ''  
  12.             }  
  13.         },  
  14.         mounted: function () {  
  15.             this.srcD = this.src  
  16.         },  
  17.         watch: {  
  18.             srcD: function (newV, oldV) {  
  19.                 console.info('src:' + this.srcD)  
  20.                 var options = {  
  21.                     method: 'GET',  
  22.                     url: 'http://www.baidu.com'  
  23.                 }  
  24.                 //jquery  
  25.                 $.ajax({  
  26.                     url:  'http://127.0.0.1:8080',  
  27.                     type: 'get',  
  28.                     dataType: 'html',  
  29.                     success: function (data){  
  30.                         console.log('jquery ajax 的請求')  
  31.                         console.log(data)  
  32.                     },  
  33.                     error: function(data) {  
  34.                         console.info(data)  
  35.                     }  
  36.                 })  
  37.                 //这个是对的  
  38.                 var resource = this.$resource("http://127.0.0.1:8080")  
  39.                 resource.get().then(function (response) {  
  40.                     console.log('this.$resource 的請求')  
  41.                     console.log(response.body)  
  42.                 }).catch(function (response) {  
  43.                     console.log(response)  
  44.                 })  
  45.                 //这个也是对的  
  46.                 this.$http.get("http://127.0.0.1:8080").then(function (res) {  
  47.                             // 处理成功的结果  
  48.                             console.log('this.$http 的請求')  
  49.                             console.info(res.body)  
  50.                             $(".contentCenter").html(res.body)  
  51.                         }, function (res) {  
  52.                             console.info(res)  
  53.                         }  
  54.                 );  
  55.             }  
  56.         }  
  57.     }  


结果:

[plain] view plain copy
  1. src:http://www.baidu.com  
  2. jquery ajax 的請求  
  3. <!DOCTYPE html>  
  4. <html>  
  5.   <head>  
  6.     <meta charset="utf-8">  
  7.     <title>server</title>  
  8.   </head>  
  9.   <body>  
  10.     <div id="app"></div>  
  11.     <!-- built files will be auto injected -->  
  12.   <script type="text/javascript" src="/app.js"></script></body>  
  13. </html>  
  14.   
  15. this.$resource 的請求  
  16. <!DOCTYPE html>  
  17. <html>  
  18.   <head>  
  19.     <meta charset="utf-8">  
  20.     <title>server</title>  
  21.   </head>  
  22.   <body>  
  23.     <div id="app"></div>  
  24.     <!-- built files will be auto injected -->  
  25.   <script type="text/javascript" src="/app.js"></script></body>  
  26. </html>  
  27.   
  28. this.$http 的請求  
  29. <!DOCTYPE html>  
  30. <html>  
  31.   <head>  
  32.     <meta charset="utf-8">  
  33.     <title>server</title>  
  34.   </head>  
  35.   <body>  
  36.     <div id="app"></div>  
  37.     <!-- built files will be auto injected -->  
  38.   <script type="text/javascript" src="/app.js"></script></body>  
  39. </html>  
这个界面是Hello.vue中的Script内容

来源http://blog.csdn.net/mastershaw/article/details/55096887

vue-resource详解文档

vue.js编写方式是通过html格式调用模版(元素/标签),这里面的模版可以是template的xml格式,也可以是JavaScript代码。

组件之间的通信:

父子组件之间:

父组件数据在子组件中使用:用props属性;子组件向父组件传递信息是用events,监听子组件执行方法,然后再运行父组件的方法。


父组件中获取子组件:同ref属性js代码:parent.$refs.a;还有内容分发slot,parent.$slots.b等。

有时候非父子关系的组件也需要通信

var bus = new Vue()
// 触发组件 A 中的事件
bus.$emit('id-selected', 1)
// 在组件 B 创建的钩子中监听事件
bus.$on('id-selected', function (id) {
// ...
})

在更多复杂的情况下,你应该考虑使用专门的 状态管理模式.


0 0
原创粉丝点击