vue和axios結合的例子。

axios從數據庫內獲取數據,填充給vue內的data上。然後,data的數據就可以綁定到html上。

html代碼

 <div id="app">
                <ul>
				    <li v-for="overview in overviews">{{overview.MeterNo}}</li>
					<li v-for="item in items">{{item.MeterNo}}</li>
				</ul>
 </div>

JavaScript代碼

      var app = new Vue({
	       el:'#app',
		   data () {
					 return {
					   overviews:[],
					   items:[
								{MeterNo: "50005445",  Longitude: "110.457323"},
								{MeterNo: "50005445",Longitude: "110.457323"}
							 ]
					 }
				},
			methods:{
			         getData () {
					              var self = this;
					              axios.post('cMs.php').then(function (response) {
												 self.overviews=response.data;
											})
											.catch(function (error) {
												console.log(error);
											  }); 
					 }
			},
			mounted () {this.getData();}
	  })  

自己犯的錯誤。

1、定義mouted在methods前。

2、this是在當前function內有效,所以,嵌套function後,嵌套function下的this是當前function內的this指向,父function的this要想引用,要用另外的變量存儲。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章