利用Vue,fetch实现前后端数据交互

news/2024/7/9 23:41:55 标签: vue, javascript, css3, es6, html5

利用Vue,fetch实现前后端数据交互

利用fetch实现数据的交互,简单练习的小实例。

  1. 目录结构
  2. 目录结构图片
  3. index.html代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title></title>
    <script type="text/javascript">
    	
   		document.addEventListener('plusready', function(){
   			//console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。"
   			
   		});
   		
    </script>
	<style type="text/css">
		ul{
			width: 80vw;
		}
    	ul li{
			width: 80vw;
			height: 5vh;
    		background-color: red;
    		margin-top: 10vh;
			list-style: none;
			text-align: center;
    	}
		.tag{
			width: 20vw;
			height: 30px;
			background-color: blue;
			margin-top: 10px;
		}
		.list  img{
			width: 30px;
			height: 30px;
			position:absolute;
			left: 8px;
		}
    </style>
</head>
<body>
		<div style="width: 100px;height:100px;background:red;"><a href="list.html">list</a></div>
	<div id="app">
		<ul class="list" v-for=" item in categories ">
			
			<li>
					<img :src=item.icon >
					{{item.name}}
			</li>
			<div class="tag" v-for = "tag in item.tags">{{tag}}</div>
		</ul>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js" ></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/3.0.0/fetch.min.js" ></script>
	<!-- 兼容 feach 异步请求 -->
	<script type="text/javascript">
		const vm =  new Vue({
			el:'#app',
			data:{
				categories:[]
			},
			created() {
				 fetch('http://127.0.0.1:5500/api/index.json')
				 	.then(res => {
						 return res.json()
					 })
					.then( data=> {
						// return data;// 真正的数据
						vm.categories = data;
					})
			},
		})
	</script>
</body>
</html>
  1. index.json中数据(可自己改,或者调用后台接口)
[
    {
        "icon":"./img/1.jpg",
        "name":"技术",
        "tags":["java","php","js"]
    },
    {
        "icon":"./img/2.jpg",
        "name":"职业",
        "tags":["js","node","vue"]
    },
    {
        "icon":"./img/2.jpg",
        "name":"职业",
        "tags":["js","node","vue"]
    },
    {
        "icon":"./img/2.jpg",
        "name":"职业",
        "tags":["js","node","vue"]
    },
    {
        "icon":"./img/2.jpg",
        "name":"职业",
        "tags":["js","node","vue"]
    }
]
  1. list.html,list.json形式一样,可以参考上面来实现完成。

http://www.niftyadmin.cn/n/482878.html

相关文章

nodeJS+express+mysql模块封装之服务器渲染小demo

nodeJSexpressmysql模块封装之服务器渲染小demo 创建一个小项目 npm init下载相关中间件&#xff08;根据package.js来下载&#xff09;package.js 目录结构 index.js代码 const express require(express); const path require(path); const template require(art-templat…

HTML5使用Geoloacation API检测浏览器的支持性

HTML5使用Geoloacation API检测浏览器的支持性 在调用HTML5 Deolocation API函数前&#xff0c;需要确保浏览器支持此功能。当浏览器不支持时&#xff0c;可以提供一些替代文本&#xff0c;以提示用户升级浏览器或安装插件&#xff08;如 Gears&#xff09;来增强现有浏览器功…

weex-使用Vue.set设置属性和使用this.xxx设置属性的区别

在使用weex过程中&#xff0c;免不了要自定义组件&#xff0c;或者说封装子控件&#xff0c;这时候需要把父组件的一些值传递到子控件中并作出相应的操作&#xff0c;这时候就要使用props来进行传值&#xff0c;但是在赋值的时候&#xff0c;具体通过Vue.set来修改属性还是通过…

weex-如何自定义一个控件,并向控件中传值,接上一篇vue.set和this.xxx赋值

上一篇博客&#xff0c;博主对vue.set和this.xxx赋值属性做了说明&#xff0c;但究竟要怎么来定义属性和进行赋值呢&#xff1f;同时这里也牵涉到如何来自定义一个控件&#xff0c;类似于weex-ui中的那些写好的组件组件&#xff0c;可以直接调用的。 效果图如下&#xff1a; …

localStorage,seeionStorage API使用

localStorage&#xff0c;seeionStorage API使用 不管我们使用哪种存储方式 &#xff0c;第一步都应该判断浏览器是否支持。 localStorage是否支持浏览器的代码&#xff1a; if(window.localStorage){alert(“浏览器支持”)&#xff1b; }else{alert(“浏览器不支持”); }由于…

weex-platform里iOS项目打包报错/platforms/ios/../../start does not exist.

使用weex的同学都知道&#xff0c;要用weex打出iOS包&#xff0c;我们潜意识都认为通过&#xff1a; week platform add ios之后&#xff0c;生成的Xcode项目可以像正常iOS项目一样打包&#xff0c;但是在打包时却报错&#xff1a; /platforms/ios/../../start does not exis…

SQL server (oracle)语句练习案例

SQL server (oracle)语句练习指南 &#xff01;&#xff01;&#xff01;练习希望可以按顺序执行&#xff01;&#xff01;&#xff01; 建表(一)&#xff1a;create table Teacher( Tno integer , Tname char(6) , Title char(6), Dept char(10));create table Teacher( Tno…

pod警告/Users/Codeliu/.rvm/gems/ruby-2.5.0/gems/cocoapods-1.4.0/lib/cocoapods/executable.rb:89:

因为升级了ruby&#xff0c;重装了pod&#xff0c;所以在使用过程中pod报如下警告&#xff1a; /Users/Codeliu/.rvm/gems/ruby-2.5.0/gems/cocoapods-1.4.0/lib/cocoapods/executable.rb:89: warning: Insecure world writable dir /Users/Codeliu in PATH, mode 040777 这是…